2011-10-12 110 views
0

好吧,我有一个表格视图,可以添加和删除单元格。对于添加的每个单元格,它会在单元格中获得两个按钮,一个加减按钮,从单元格的textLabel中添加1和子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

回答

2

您应该传递该单元格作为参数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]; 
    } 
} 
+0

即时通讯相当noobish呵呵!好吧,如果我的单元格名称是单元格,我该如何编写参数?像 - (void)subtractLabelText:(UITableViewCell *)单元格?还是有另一种方式?非常感谢!! :D – iProRage

+0

@iProRage查看我的更新。请停止转贴此评论。 – PengOne

+0

作品完美!非常感谢!! :D:D – iProRage

相关问题