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

标签 ios iphone uitableview ios7

我有带有搜索栏的tableview。我正在显示单元格中的数据数量。我正在使用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;

最佳答案

您必须将要摆脱的nil值或将可重复的代码插入可重用性if(这是:if (cell == nil) {
这里的主要规则是创建一个必须将两种对象分开的单元:
-每个单元格唯一的对象(文本)
-重复的对象(主要是样式对象,例如颜色,背景,阴影,按钮和填充物)

第一组应该在外部可重用性“if” (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;

关于ios - 如何从单元格中删除以前的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20608992/

相关文章:

ios - 在 Swift 中连接到互联网时,所有用户如何将 UIViews 发布到单个流?

iphone - 使用 ASIHTTPRequest 的 UITableViewCell 中的 UIImageView 性能

swift - UITableView 删除了在 canEditRowAt 中不可编辑的行上的披露指示符和选择

ios - 从单元格中获取标签 - swift

ios - 以编程方式刷新 mapview 用户位置

ios - UICollectionView performBatchUpdates 导致 collectionview 在旋转设备时错位

iphone - 来自 UITapGestureRecognizer 的 CGPoint

iphone - UITextField 的 iOS 5 和 iOS 6 行为在 KeyboardType Email 的情况下不同

iphone - 将 NSDefaults 字符串添加到 InAppSettingsKit 中

iphone - Objective-C 如何有选择地禁用用户交互