无法更新现有单元格中的UILabel文本

时间:2010-12-17 22:44:45

标签: iphone objective-c cocoa-touch uikit

这看起来应该很容易,但我无法让它发挥作用。我已经看过很多类似的帖子,并尝试了解决方案,但没有一个对我有用。

基本上,我正在尝试更新单元格中标签的文本。它在我第一次创建单元格时起作用,但是当我从dequeueReusableCellWithIdentifier获取单元格时,它不起作用。顺便说一句,我实际上使用的是DTGridView,它有点类似于UITableView。以下是我的视图控制器中的相关代码:

- (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";
    }
}

以下是EntityViewCell类的相关代码:

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

单元格的标题已正确设置为我的data数组中的值,但在选择单元格后它永远不会更改为SELECTED。我已经通过调试验证,当一个单元被触摸时,setTitle被调用aTitle设置为SELECTED,所以我不确定为什么视图没有显示这个。

谢谢!

2 个答案:

答案 0 :(得分:0)

在上面的代码中,您正在更改属性标题,在较低的代码中,您正在更改属性文本。这可能是问题吗?

答案 1 :(得分:0)

我可以通过将[self setNeedsDisplay]添加到DTDataGrid reloadData函数的末尾来解决此问题。我不确定为什么需要它(并将其添加到我的单元格setTitle不起作用)。

我有点担心这里的效率,但我不知道有更好的方法来解决这个问题(我对iOS很新)。现在,我每次选择一个单元格时都会调用reloadData,因为我将setNeedsDisplay添加到DTDataGrid,所以每次都会重绘整个数据网格。必须有更好的方法......任何想法?