我有一個UITableView,我可以添加和刪除單元格到這個表。我也有兩個按鈕。 1按鈕爲單元格的文本添加「1」,這是1,所以基本上按下+按鈕時會計數,按下按鈕時會減小。我的問題是與最後一個單元格。如果我添加5個單元格,它是有問題的第5個單元格。如果我添加了200個單元格,它的第200個單元格等。問題是當我按下 - 按鈕時,所有其他單元格在按下時保持轉動藍色,並且此按鈕停止轉動藍色。當單元格文本爲0時,它會保持白色。我希望它在按下時保持與所有其他單元格一樣變藍。這裏是我的代碼:tableView中的奇怪的bug
- (IBAction)subtractLabelText:(id)sender
{
cell = (UITableViewCell*)[sender superview];
if ([[cell.textLabel text] intValue] == 0){
[newBtn setEnabled:NO];
}
else{
cell.textLabel.text = [NSString stringWithFormat:@"%d",[cell.textLabel.text
intValue] -1];
[newBtn setEnabled:YES];
}
}
此方法連接到子「 - 」按鈕。另外,當我按下按鈕時,文本是= 0,按鈕是在那裏,但是當我按下它,它選擇單元格和表格單元格變成藍色,就好像我選擇了!請幫忙!謝謝大家!
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(260,20,55,35)];
[newBtn addTarget:self action:@selector(subtractLabelText:)
forControlEvents:UIControlEventTouchUpInside];
[newBtn setTitle:@"-" forState:UIControlStateNormal];
[newBtn setEnabled:YES];
[cell addSubview:newBtn];
subBtn = [[UIButton alloc]init];
subBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[subBtn setFrame:CGRectMake(200,20,55,35)];
[subBtn addTarget:self action:@selector(addLabelText:)
forControlEvents:UIControlEventTouchUpInside];
[subBtn setTitle:@"+" forState:UIControlStateNormal];
[subBtn setEnabled:YES];
[cell addSubview:subBtn];
return cell;
}
可以請您發佈'-cellForRowAtIndexPath'方法中的代碼 –
是的,我很喜歡! – iProRage