开发者

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:(NS开发者_Python百科Integer)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?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜