Iphone 表格视图单元格';s按钮与其他单元格发生冲突?
好的,我有一个可以添加和删除单元格的表视图。对于添加的每个单元格,它都会在单元格中获得两个按钮,一个“添加”和“减去”按钮,用于从单元格的文本标签中添加1和subs 1。但当我按下1个单元格按钮时,它会从另一个单元格文本标签中删除。这是我的Iphone 表格视图单元格';s按钮与其他单元格发生冲突?,iphone,ios,cocoa-touch,uitableview,uibutton,Iphone,Ios,Cocoa Touch,Uitableview,Uibutton,好的,我有一个可以添加和删除单元格的表视图。对于添加的每个单元格,它都会在单元格中获得两个按钮,一个“添加”和“减去”按钮,用于从单元格的文本标签中添加1和subs 1。但当我按下1个单元格按钮时,它会从另一个单元格文本标签中删除。这是我的cellForRow方法: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath { static
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];
}
}
-(iAction)subtractLabelText:(id)发送方{
UITableViewCell*cell=(UITableViewCell*)[sender superview];//在编码方面,我非常不懂,哦!!好的,如果我的单元格名为cell,我应该如何写参数?比如-(void)subtractLabelText:(UITableViewCell*)cell?或者有其他方法吗?非常感谢!!:D