表格视图单元格的按钮与其他单元格混淆?
好的,我有一个可以添加和删除单元格的表视图。对于添加的每个单元格,它会在单元格中获得两个按钮,一个加减按钮,用于从单元格的文本标签中添加 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该将单元格作为
subtractLabelText:
的参数传递,否则该函数不知道它访问的是哪个单元格。我想到的最简单的事情就是添加一行定义单元格:
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: