[英]Custom UITableviewcell updates in hidden cells
我使用了自定義UITableViewCell。 我在其中添加了手勢手勢選項,在tableView
有20個單元格。 如果我滑動第一個單元格並滾動,則意味着第11個單元格也更新為我的第一個單元格值。
以下是我的代碼片段。
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell*)
[tableView dequeueReusableCellWithIdentifier:@"CustomCellId”];
[cell setRequestCellDelegate:self];
[cell.swipeLeft addTarget:self action:@selector(swipeLeftAction:)];
cell.tag = indexPath.row;
cell.swipeLeft.delegate = self;
cell.swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
cell.indexpath = indexPath;
[cell.swipeRight addTarget:self action:@selector(swipeRightAction:)];
cell.swipeRight.delegate = self;
cell.swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
cell.tableHoldButtn.tag = indexPath.row;
return cell;
}
請幫助找到解決方案。
之所以發生這種情況是因為,一旦您的第一個滑動單元離開了屏幕,它就會被放入隊列以供重用。 然后,當第10個單元格出現在屏幕上時,它不是在創建中,而是第一個單元格正在重用。 並且由於您已對其進行了刷卡,因此它將以與離開屏幕時完全相同的狀態出隊。
您應該在表視圖控制器中跟蹤應擦除的單元格的更改,並在cellForIndexPath數據源方法中恢復該狀態。
我認為最好在使用手勢時使用tableView委托方法。 您可以使用
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
此外,您還可以通過以下方式自定義編輯按鈕的外觀:
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath;
CustomCell *cell = (CustomCell*)
[tableView dequeueReusableCellWithIdentifier:[NSString stringwithFormat:"%ld",indexPath.row]];
重用標識符更改為indexPath.row的字符串
希望我的實驗能對您有所幫助
注意:不要在其他地方注冊單元格,在其他地方刪除注冊單元格(也許在viewdidLoad中)
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// CustomCell *cell = (CustomCell*)
// [tableView dequeueReusableCellWithIdentifier:@"CustomCellId”];
// need not register cell in other ,like viewdidLoad
CustomCell* cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%@",indexPath.row]];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:@"%@",indexPath.row]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
[cell setRequestCellDelegate:self];
[cell.swipeLeft addTarget:self action:@selector(swipeLeftAction:)];
cell.tag = indexPath.row;
cell.swipeLeft.delegate = self;
cell.swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
cell.indexpath = indexPath;
[cell.swipeRight addTarget:self action:@selector(swipeRightAction:)];
cell.swipeRight.delegate = self;
cell.swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
cell.tableHoldButtn.tag = indexPath.row;
return cell;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.