简体   繁体   中英

Can't update UILabel text in existing cell

This seems like it should be really easy, but I can't get it to work. I've seen a lot of similar posts, and tried the solutions, but none of them have worked for me.

Basically, I'm trying to update the text of a label in a cell. It works the first time I create the cell, but doesn't work subsequently when I get the cell from dequeueReusableCellWithIdentifier . By the way, I'm actually using a DTGridView , which is somewhat similar to a UITableView . Here is the relevant code from my view controller:

- (DTGridViewCell *)gridView:(DTGridView *)gv viewForRow:(NSInteger)rowIndex column:(NSInteger)columnIndex {

    EntityViewCell *cell = [self entityViewCellForGridView:gv];

    // Find the single dimension index of the cell
    NSUInteger index = [self indexOfGridView:gv row:rowIndex column:columnIndex];

    [self configureCell:cell atIndex:index];

    return cell;
}

- (EntityViewCell *)entityViewCellForGridView:(DTGridView *)gv {
    NSString *CellIdentifier = @"EntityViewCell";

    EntityViewCell *cell = (EntityViewCell *)[gv dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {
        cell = (EntityViewCell *)[EntityViewCell loadCellWithReuseIdentifier:CellIdentifier];
    }
    return cell;
}

- (void)configureCell:(EntityViewCell *)cell atIndex:(NSInteger)index {
    NSString *name = [[self.data objectAtIndex:index] valueForKey:@"name"];
    if (!name) {
        name = @"";
    }
    cell.title = name;

    // Change the title of the cell if it is selected
    if ([self.selectedCells objectForKey:[NSNumber numberWithInt:index]] != nil) {
        cell.title = @"SELECTED";
    }
}

And here is the relevant code from the EntityViewCell class:

- (void) setTitle: (NSString *) aTitle {
    if (!label) {
        label = [[UILabel alloc] initWithFrame: CGRectZero];
    }
    label.text = aTitle; 
    [self setNeedsLayout];
}

The cell's title is correctly set to the value in my data array, but it never changes to SELECTED after selecting a cell. I have verified through debugging that setTitle is being called with aTitle set to SELECTED when a cell has been touched, so I'm not sure why the view is not showing this.

Thanks!

In the upper code you are changing the property title and in the lower code you are changing the property text. Could this be the problem?

I was able to solve this by adding [self setNeedsDisplay] to the end of the DTDataGrid reloadData function. I'm not sure why this is needed (and adding it to my cell's setTitle doesn't work).

I'm a little worried about the efficiency here, but I don't know of a better way to handle this (I'm pretty new to iOS). Right now, I call reloadData every time a cell is selected, and since I added setNeedsDisplay to the DTDataGrid , it redraws the entire data grid every time. There must be a better way... any ideas?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM