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

时间:2013-12-16 10:48:24

标签: ios iphone uitableview ios7

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

enter image description here

2)搜索数据后:

![enter image description here][2]

enter image description here

我的代码:

- (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;

3 个答案:

答案 0 :(得分:1)

您需要删除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;

答案 1 :(得分:0)

您的代码中存在两个问题:

  1. 您为doneButtonpreviewButton设置了标记,但永远不会使用该标记。那么标签用于什么?

  2. 滚动表格视图时,将重复使用表格视图单元格。例如,如果单元格出现在索引0处,则在索引10处重复使用,作为代码,将再次添加按钮。

  3. 因此,您可以为所有按钮单独设置一个唯一标记,每次调用- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath时,都应检查是否添加了按钮,如果是,则应避免再次添加按钮。

    您可以添加以下代码:

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

答案 2 :(得分:-1)

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

只需致电[cell.contentView removeSubViews];

这应该有帮助