开发者

Custom UITableViewCell Not updating on Search

开发者 https://www.devze.com 2023-03-21 09:49 出处:网络
I\'m using a uitableviewcontroller with a uiSearchBar. In the searchBarSearchButtonClicked method I get data and populate it in the uitableviewcontroller:

I'm using a uitableviewcontroller with a uiSearchBar. In the searchBarSearchButtonClicked method I get data and populate it in the uitableviewcontroller:

 - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {

    NSDictionary *data = [self getData];

    [self.tableData removeAllObjects];
    [self.tableData addObjectsFromArray:[data objectForKey:@"myKey"]];
    [self.view reloa开发者_StackOverflow社区dData];
 }  

The problems is that I am using a custom uitableviewcell. When I click the search button new rows are returned, but the rows retrieved when the previous search was ran still remain.

For example if my initial search returned 1 result row and I do an additional search this time returning 3 rows, the first row will be from the first search and the subsequent two rows will be from the second search.

I reload the data in the view so I'm not sure why the cells are persisting. Here is my cellforrowatindexpath method where I create the cells:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 static NSString *MyIdentifier = @"SearchResult";

UITableViewCell *cell = [tableView
                         dequeueReusableCellWithIdentifier:MyIdentifier];

NSDictionary *data = [self.tableData objectAtIndex:indexPath.row];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] 
             initWithStyle:UITableViewCellStyleSubtitle 
             reuseIdentifier:MyIdentifier] autorelease];



myLabelDetail = [[UILabel alloc] initWithFrame:CGRectMake(152.0, 32.0, 60.0, 5.0)];
myLabelDetail.tag = INSTRUMENT_ID_LABEL_TAG;
myLabelDetail.font = [UIFont systemFontOfSize:14.0];
myLabelDetail.textAlignment = UITextAlignmentLeft;
myLabelDetail.textColor = [UIColor grayColor];
myLabelDetail.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
myLabelDetail.text = [data objectForKey:@"title"];
[cell.contentView addSubview:myLabelDetail];
[myLabelDetail release];



cell.imageView.image = [UIImage imageNamed:@"icon"];
cell.textLabel.text = @"Title Text";
cell.detailTextLabel.text = @"My Label: ";


cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}else{
    myLabelDetail = (UILabel *)[cell.contentView viewWithTag:DETAIL_LABEL_TAG];
}

return cell;
}


I think the problem is in using the data source. I guess tableData is the array you use for providing data to the tableview as it is flushed and loaded with new objects every time the - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar method is called. But tableData is not used anywhere inside cellForRowAtIndexPath method. Providing data to tableView from tableData array may solve your problem.


I figured it out:

 }else{
      myLabelDetail = (UILabel *)[cell.contentView viewWithTag:DETAIL_LABEL_TAG];
      // i needed to add this to update when cell is already initialized
      myLabelDetail = [data objectForKey:@"title"];
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号