私はUITableViewを持っています。このテーブルにセルを追加したり削除したりできます。私は2つのボタンも持っています。 1ボタンはセルのテキストに "1"を追加します。これは1で、基本的に+ボタンを押すとカウントし、 - ボタンを押すとカウントします。私の問題は最後のセルにあります。 5つのセルを追加すると、問題のある5番目のセルが追加されます。私が200のセルを200番目のセルに追加すると、問題は私が - ボタンを押すと、他のすべてのセルが押されて青くなり、このボタンが青くなります。セルのテキストが0のときに押しても白くなります。押したときに他のセルのように青色に変わらないようにします。ここに私のコードです:tableViewの奇妙なバグ
- (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:UIは「について知って」いないという点での推測では
- (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