gpt4 book ai didi

iphone - 表格 View 单元格按钮与其他单元格混淆?

转载 作者:行者123 更新时间:2023-11-28 23:12:40 25 4
gpt4 key购买 nike

好的,我有一个可以添加和删除单元格的 TableView 。对于添加的每个单元格,它在单元格中有两个按钮,一个加减按钮,从单元格的文本标签中添加 1 和 subs 1。但是当我按下 1 个单元格按钮时,它会从另一个单元格 textLabel 中替换。这是我的 cellForRow 方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    
(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
cell.imageView.image = [imageArray objectAtIndex:indexPath.row];
cell.textLabel.text = [cells objectAtIndex:indexPath.row];

newBtn = [[UIButton alloc]init];
newBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[newBtn setFrame:CGRectMake(195,20,55,35)];
[newBtn addTarget:self action:@selector(subtractLabelText:)
forControlEvents:UIControlEventTouchUpInside];
[newBtn setTitle:@"-" forState:UIControlStateNormal];
[cell addSubview:newBtn];

return cell;
}

下一个方法是我连接到减法按钮的方法:

- (IBAction)subtractLabelText:(id)sender{

if ( [[cell.textLabel text] intValue] == 0){
[newBtn setEnabled:NO];
}
else{
cell.textLabel.text = [NSString stringWithFormat:@"%d",[cell.textLabel.text
intValue] -1];
}
}

请帮忙!!非常感谢! :D

最佳答案

您应该将单元格作为 subtractLabelText: 的参数传递,否则该函数不知道它访问的是哪个单元格。


我想到的最简单的事情就是添加一行定义单元格:

- (IBAction)subtractLabelText:(id)sender{

UITableViewCell *cell = (UITableViewCell*)[sender superview]; // <-- ADD THIS LINE

if ( [[cell.textLabel text] intValue] == 0){
[newBtn setEnabled:NO];
}
else{
cell.textLabel.text = [NSString stringWithFormat:@"%d",[cell.textLabel.text
intValue] -1];
}
}

关于iphone - 表格 View 单元格按钮与其他单元格混淆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7734044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com