Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从单元格中删除以前的内容_Ios_Iphone_Uitableview_Ios7 - Fatal编程技术网

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

Ios 如何从单元格中删除以前的内容,ios,iphone,uitableview,ios7,Ios,Iphone,Uitableview,Ios7,我有带搜索栏的tableview。我正在显示单元格中的数据数。我正在使用xib创建细胞。我还在单元格中添加了两个按钮。现在,当我在搜索栏中搜索任何东西时,时间表正在重新加载,单元格按钮再次添加到该位置。 请参见以下屏幕截图: 1) 第一次创建tableview: 2) 搜索后数据: ![enter image description here][2] 我的代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForR

我有带搜索栏的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;

重新加载tableview时,需要删除作为子视图添加的按钮 因此,在您的
-(UITableViewCell*)表视图中:(UITableView*)表视图cellforrowatinexpath:(nsindepath*)indepath{

只需调用
[cell.contentView removeSubViews];


这应该会有所帮助,代码中有两个问题:

  • 您为
    doneButton
    previewButton
    设置了标记,但该标记从未被使用过。那么该标记的用途是什么

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

  • 因此,您可以为所有按钮分别设置一个唯一的标记,并且每次调用
    -(UITableViewCell*)tableView:(UITableView*)tableView cellforrowatinexpath:(nsindepath*)indepath
    ,您都应该检查按钮是否已添加,如果已添加,则应避免再次添加按钮

    您可以添加如下代码:

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

    他不需要可重用性,如果你明白这不是可重用性的问题,他每次都在添加内容,这样你就可以删除不需要的内容,这样按钮就不会像图片中那样被添加两次。每个人都需要可重用性!如果你每次设置单元格的样式,滚动会出现可怕的滞后。更改文本每次都是肯定的,重画按钮-不!好的,我想要完全一样的东西…谢谢…我明白你的意思了