![](/img/trans.png)
[英]Updating alternating colored UITableViewCells when rows gets reordered or deleted
[英]UITableViewCells text label gets set to a cell when the cell is deleted
我有一個UItableView我可以添加和刪除單元格。 我可以更改cell.textLabel.text
屬性。 如果我將13添加到我的cell.textLabel.text
...
如果我刪除TOP單元格,那個單元格的textLabel
將設置為向上移動的單元格...
我怎樣才能解決這個問題? 我希望能夠刪除一個單元格,讓它保持自己的textLabel
。 如果需要,這是我的cellForRow
方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *identifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
addBtn = [[UIButton alloc]init];
addBtn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
[addBtn setFrame:CGRectMake(220,10,25,55)];
[addBtn addTarget:self action:@selector(addLabelText:) forControlEvents:UIControlEventTouchUpInside];
[addBtn setTitle:@"+" forState:UIControlStateNormal];
[addBtn setEnabled:YES];
[cell addSubview:addBtn];
subBtn = [[UIButton alloc]init];
subBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[subBtn setFrame:CGRectMake(260,10,25,55)];
[subBtn addTarget:self action:@selector(subtractLabelText:) forControlEvents:UIControlEventTouchUpInside];
[subBtn setTitle:@"-" forState:UIControlStateNormal];
[subBtn setEnabled:YES];
[cell addSubview:subBtn];
}
//cellText.hidden=!self.editing;
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.imageView.image = [imageArray objectAtIndex:indexPath.row];
cell.tag = indexPath.row; // Important for identifying the cell easily later
cell.textLabel.text = [self.number objectAtIndex:indexPath.row];
return cell;
}
在此先感謝您的幫助!
刪除單元格時, UITableView
及其中的所有單元格都會重新繪制。 刪除單元格時,您還需要從數據源中刪除相應的對象(在您的情況下,數組self.number
),以便數據源數組中的對象對應表格單元格的新索引。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.