表格视图单元格的按钮与其他单元格混淆?

发布于 2024-12-09 05:40:13 字数 1303 浏览 0 评论 0原文

好的,我有一个可以添加和删除单元格的表视图。对于添加的每个单元格,它会在单元格中获得两个按钮,一个加减按钮,用于从单元格的文本标签中添加 1 和 subs 1。但是当我按下 1 个单元格按钮时,它会从另一个单元格文本标签中进行订阅。这是我的 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

Ok i have a table view that can add and delete cells. For each cell thats added, it gets two buttons in the cell an add and subtract button which adds 1 and subs 1 from the cell's textLabel. But when i press on 1 cells button, it subs from another cells textLabel. Here is my cellForRow method:

- (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;
}

This next method is the method is the one i hooked up to the subtract button:

- (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];
    }
}

Please help!! Thanks so much! :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮筵 2024-12-16 05:40:13

您应该将单元格作为 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];
    }
}

You should pass the cell as the argument of subtractLabelText:, otherwise this function does not know which cell its accessing.


The simplest thing that comes to mind for me is to do is to add in a line defining the cell:

- (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];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文