如何从单元格中删除以前的内容

Posted

技术标签:

【中文标题】如何从单元格中删除以前的内容【英文标题】:How to remove previous content from cell 【发布时间】:2013-12-16 10:48:24 【问题描述】:

我有带搜索栏的表格视图。我在单元格中显示数据数量。我正在使用 xib 创建单元格。我还在单元格中添加了两个按钮。现在,当我在搜索栏中搜索任何内容时,表格正在重新加载,并且单元格按钮正在该位置再次添加。 请参阅下面的屏幕截图: 1)第一次创建tableview:

2)搜索数据后:

![enter image description here][2]

我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
WorkDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WorkDetailCell"];
if (cell == nil) 
    NSArray *topLevelObject;
    topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"WorkDetailCell" owner:self options:nil];
    cell = [topLevelObject objectAtIndex:0];
    cell.backgroundColor=RGB(210, 200, 191);
    cell.selectedBackgroundView=[self selectedCellView];


if(search==FALSE)
    NSLog(@"%d",indexPath.row);
    cell.clearsContextBeforeDrawing=YES;
    //NSDictionary *detailList=[tempDataArray objectAtIndex:indexPath.row];
    NSArray *temp=[tempDataArray objectAtIndex:indexPath.row];
    cell.lblOrganization.text=[temp objectAtIndex:1];//@"TGB";//[detailList valueForKey:@"organizationName"];
    cell.lblAddress.text=[temp objectAtIndex:2];//@"Ahmedabad";//detailList[@"address"];
    cell.lblLandmark.text=[temp objectAtIndex:3];//@"SG HighWay";//detailList[@"landmark"];
    cell.lblCity.text=[temp objectAtIndex:4];//@"Ahmedabad";//detailList[@"city"];
    cell.lblState.text=[temp objectAtIndex:5];//@"Ahmedabad";//detailList[@"state"];


    UIButton *doneButton=[UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame=CGRectMake(220, 45, 100, 60);
  //[doneButton setTitle:@"pick" forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(doneButtonClicked:) fo rControlEvents:UIControlEventTouchUpInside];
   [doneButton setImage:[UIImage imageNamed:@"pick"] forState:UIControlStateNormal];
    [doneButton setTag:indexPath.row];
   UIButton *previewButton=[UIButton buttonWithType:UIButtonTypeCustom];
    previewButton.frame=CGRectMake(235, 15, 65, 30);
    [previewButton setTitle:@"Preview" forState:UIControlStateNormal];
    [previewButton setTitleColor:RGB(110, 73, 44) forState:UIControlStateNormal];
    [previewButton addTarget:self action:@selector(previewButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [previewButton setTag:indexPath.row];
    [cell.contentView addSubview:doneButton];
    [cell.contentView addSubview:previewButton];

else
 //   NSDictionary *detailList=[searchResultArray objectAtIndex:indexPath.row];
   // NSLog(@"dic%@",detailList);
    //cell.contentView.clearsContextBeforeDrawing=YES;
  cell.clearsContextBeforeDrawing=YES;
    NSArray *temp=[searchResultArray objectAtIndex:indexPath.row];
    cell.lblOrganization.text= [temp objectAtIndex:1]; //[detailList valueForKey:@"organizationName"];
    cell.lblAddress.text=[temp objectAtIndex:2];//detailList[@"address"];
    cell.lblLandmark.text=[temp objectAtIndex:3];//detailList[@"landmark"];
    cell.lblCity.text=[temp objectAtIndex:4];//detailList[@"city"];
    cell.lblState.text=[temp objectAtIndex:5];//detailList[@"state"];
    UIButton *doneButton=[UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame=CGRectMake(220, 45, 100, 60);
  //[doneButton setTitle:@"pick" forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(doneButtonClicked:) fo rControlEvents:UIControlEventTouchUpInside];
   [doneButton setImage:[UIImage imageNamed:@"pick"] forState:UIControlStateNormal];
    [doneButton setTag:indexPath.row];
   UIButton *previewButton=[UIButton buttonWithType:UIButtonTypeCustom];
    previewButton.frame=CGRectMake(235, 15, 65, 30);
    [previewButton setTitle:@"Preview" forState:UIControlStateNormal];
    [previewButton setTitleColor:RGB(110, 73, 44) forState:UIControlStateNormal];
    [previewButton addTarget:self action:@selector(previewButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [previewButton setTag:indexPath.row];
    [cell.contentView addSubview:doneButton];
    [cell.contentView addSubview:previewButton];
   
   return cell;

【问题讨论】:

【参考方案1】:

你必须 nil 要摆脱的值或插入可重复代码以实现可重用性 if(这是:if (cell == nil)

这里的主要规则是创建一个必须分隔两种对象的单元格: - 每个单元格独有的对象(例如文本) - 重复对象(主要是样式对象,如颜色、背景、阴影、按钮等)

第一组应该在可重用性之外“如果”(cell == nil)。第二个应该在里面。

在你的情况下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
WorkDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WorkDetailCell"];
if (cell == nil) 
    NSArray *topLevelObject;
    topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"WorkDetailCell" owner:self options:nil];
    cell = [topLevelObject objectAtIndex:0];
    cell.backgroundColor=RGB(210, 200, 191);
    cell.selectedBackgroundView=[self selectedCellView];

   UIButton *doneButton=[UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame=CGRectMake(220, 45, 100, 60);
  //[doneButton setTitle:@"pick" forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(doneButtonClicked:) fo rControlEvents:UIControlEventTouchUpInside];
   [doneButton setImage:[UIImage imageNamed:@"pick"] forState:UIControlStateNormal];
    [doneButton setTag:indexPath.row];
   UIButton *previewButton=[UIButton buttonWithType:UIButtonTypeCustom];
    previewButton.frame=CGRectMake(235, 15, 65, 30);
    [previewButton setTitle:@"Preview" forState:UIControlStateNormal];
    [previewButton setTitleColor:RGB(110, 73, 44) forState:UIControlStateNormal];
    [previewButton addTarget:self action:@selector(previewButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:doneButton];
    [cell.contentView addSubview:previewButton];


if(search==FALSE)
    NSLog(@"%d",indexPath.row);
    cell.clearsContextBeforeDrawing=YES;
    //NSDictionary *detailList=[tempDataArray objectAtIndex:indexPath.row];
    NSArray *temp=[tempDataArray objectAtIndex:indexPath.row];
    cell.lblOrganization.text=[temp objectAtIndex:1];//@"TGB";//[detailList valueForKey:@"organizationName"];
    cell.lblAddress.text=[temp objectAtIndex:2];//@"Ahmedabad";//detailList[@"address"];
    cell.lblLandmark.text=[temp objectAtIndex:3];//@"SG HighWay";//detailList[@"landmark"];
    cell.lblCity.text=[temp objectAtIndex:4];//@"Ahmedabad";//detailList[@"city"];
    cell.lblState.text=[temp objectAtIndex:5];//@"Ahmedabad";//detailList[@"state"];


    [previewButton setTag:indexPath.row];


else
 //   NSDictionary *detailList=[searchResultArray objectAtIndex:indexPath.row];
   // NSLog(@"dic%@",detailList);
    //cell.contentView.clearsContextBeforeDrawing=YES;
  cell.clearsContextBeforeDrawing=YES;
    NSArray *temp=[searchResultArray objectAtIndex:indexPath.row];
    cell.lblOrganization.text= [temp objectAtIndex:1]; //[detailList valueForKey:@"organizationName"];
    cell.lblAddress.text=[temp objectAtIndex:2];//detailList[@"address"];
    cell.lblLandmark.text=[temp objectAtIndex:3];//detailList[@"landmark"];
    cell.lblCity.text=[temp objectAtIndex:4];//detailList[@"city"];
    cell.lblState.text=[temp objectAtIndex:5];//detailList[@"state"];

    [previewButton setTag:indexPath.row];
   
   return cell;

【讨论】:

好的,我想要完全相同的东西......谢谢......我明白你的意思【参考方案2】:

你的代码有两个问题:

    您为doneButtonpreviewButton 设置了标签,但从未使用过该标签。那么这个标签是干什么用的呢?

    当您滚动表格视图时,表格视图单元格将被重复使用。例如,如果出现在索引 0 的单元格在索引 10 处被重用,作为您的代码,按钮将被重新添加。

所以,你可以为所有按钮单独设置一个唯一的标签,每次调用- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath时,你应该检查按钮是否被添加,如果是,你应该避免再次添加按钮。

您可以添加如下代码:

    if (![cell viewWithTag:YOUR_BUTTON_TAG]) 
        //create and add your button
    

【讨论】:

【参考方案3】:

当您重新加载 tableview 时,您需要删除作为子视图添加的按钮 所以在你的- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

只需拨打[cell.contentView removeSubViews];

这应该会有所帮助

【讨论】:

他不需要可重用性,如果你理解这不是可重用性的问题,他每次都在添加内容,所以你可以删除不需要的东西,所以按钮不会被添加两次在图片中 每个人都需要可重用性!如果您每次都对单元格进行样式设置,那么滚动就会出现可怕的滞后。每次确定都更改文本,重绘按钮 - 不!

以上是关于如何从单元格中删除以前的内容的主要内容,如果未能解决你的问题,请参考以下文章

单击单元格文本时如何从单元格中删除文本

如何从uitableview单元格中删除单元格

为啥我不能从情节提要的表格单元格中删除标签?

如何从 QGridLayout 中的单元格中删除小部件

如何检测是不是在多个彼此不相邻的单元格中删除了文本

如何从分组的 UITableView 单元格中删除边框?