tech_human
tech_human

Reputation: 7144

UITableViewCell not displaying text correctly

I have a list of data from which I search for some items and then I select one of them which expand the selected cell. Then when I clear out the search text in search bar, I wish to display the original list. I am able to display all the items in the original list, except for the cell that was selected during search doesn't get updated. Attaching snapshots and code for better understanding:

Original List

Search for text and select item on row 3

Clear search and display original list.

Notice that row #3 in first image has text "A.B. Road" whereas the same row in third image uses the same cell as in second image (it should get updated to "A.B. Road") I am using a custom cell and tried creating a new cell everytime instead of reusing the existing cell. This didn't help.

Code:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText = [self.branchList objectAtIndex:[indexPath row]];
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];

    CGSize labelSize = [cellText boundingRectWithSize:CGSizeMake(tableView.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:cellFont} context:nil].size;

    CGFloat cellHeight = labelSize.height + 20;
    return [self.expandedCells containsObject:indexPath] ? cellHeight * 5 : cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *stateListCellId = @"stateList";

    BranchDetailsTableViewCell *stateListCell = [tableView dequeueReusableCellWithIdentifier:stateListCellId];

    if (!stateListCell) {
        [tableView registerNib:[UINib nibWithNibName:@"BranchDetailsTableViewCell" bundle:nil] forCellReuseIdentifier:stateListCellId];
        stateListCell = [tableView dequeueReusableCellWithIdentifier:stateListCellId];
    }

    return stateListCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.expandedCells containsObject:indexPath]) {
        [self.expandedCells removeObject:indexPath];

        [self.tableView beginUpdates];
        [self.tableView reloadRowsAtIndexPaths:@[self.selectedIndexPath] withRowAnimation:UITableViewRowAnimationNone];
        [self.tableView endUpdates];
    }
    else {
        [self.activityIndicator showActivityIndicatorForView:self.navigationController.view];
        self.selectedIndexPath = indexPath;
        [self.expandedCells addObject:indexPath];
        [self getDataFromService];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(BranchDetailsTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    [self displayDataOnTheCell];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    if ([self.tableView numberOfRowsInSection:0] != [self.branchListCopy count]) {
        [self.expandedCells removeAllObjects];
// Copy the original list to display.
        self.branchList = self.branchListCopy;
        [self.tableView reloadData];
    }
}

It looks like it's just the cell that is not getting rendered again because when I debug, I do see "A.B. Road" in the array, it's the cell that is not displaying it. I tried calling "[cell setNeedsDisplay]" and also creating new cell always instead of reusing, but nothing helped. What else could help?

Thanks!

Upvotes: 0

Views: 68

Answers (2)

tech_human
tech_human

Reputation: 7144

I was able to get it work by replacing the below code with [tableview reloadData] in didSelectRowAtIndexPath method keeping rest of the code as is.

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[self.selectedIndexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];

Final working solution:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText = [self.branchList objectAtIndex:[indexPath row]];
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];

    CGSize labelSize = [cellText boundingRectWithSize:CGSizeMake(tableView.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:cellFont} context:nil].size;

    CGFloat cellHeight = labelSize.height + 20;
    return [self.expandedCells containsObject:indexPath] ? cellHeight * 5 : cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *stateListCellId = @"stateList";

    BranchDetailsTableViewCell *stateListCell = [tableView dequeueReusableCellWithIdentifier:stateListCellId];

    if (!stateListCell) {
        [tableView registerNib:[UINib nibWithNibName:@"BranchDetailsTableViewCell" bundle:nil] forCellReuseIdentifier:stateListCellId];
        stateListCell = [tableView dequeueReusableCellWithIdentifier:stateListCellId];
    }

    return stateListCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.expandedCells containsObject:indexPath]) {
        [self.expandedCells removeObject:indexPath];

        [self.tableView reloadData];
    }
    else {
        [self.activityIndicator showActivityIndicatorForView:self.navigationController.view];
        self.selectedIndexPath = indexPath;
        [self.expandedCells addObject:indexPath];
        [self getDataFromService];
    }
}

Upvotes: 1

Bohm
Bohm

Reputation: 1004

I think the problem is that you are using the same cell identifier for each cell. Try to use different cell identifiers:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//NSString *stateListCellId = @"stateList";

    NSString *cellIdentifier = [NSString stringWithFormat:@"cell%ld",(long)indexPath.row];
    BranchDetailsTableViewCell *stateListCell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];

    if (!stateListCell) {
        [tableView registerNib:[UINib nibWithNibName:@"BranchDetailsTableViewCell" bundle:nil] forCellReuseIdentifier:stateListCellId];
        stateListCell = [tableView dequeueReusableCellWithIdentifier:stateListCellId];
    }

    return stateListCell;
}

Upvotes: 0

Related Questions