【发布时间】:2011-10-12 00:29:33
【问题描述】:
好的,我有一个可以添加和删除单元格的表格视图。对于添加的每个单元格,它会在单元格中获得两个按钮,一个添加和减去按钮,该按钮从单元格的 textLabel 中添加 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
【问题讨论】:
标签: iphone ios cocoa-touch uitableview uibutton