Кажется, это должно быть действительно легко, но я не могу заставить его работать. Я видел много похожих постов и пробовал решения, но ни один из них не помог мне.
Обычно я пытаюсь обновить текст метки в ячейке. Он работает в первый раз, когда я создаю ячейку, но не работает впоследствии, когда я получаю ячейку из 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
, но оно никогда не изменяется на ВЫБРАННОЕ после выбора ячейки. В ходе отладки я подтвердил, что setTitle
вызывается с aTitle
, установленным в значение SELECTED, когда была затронута ячейка, поэтому я не уверен, почему представление не показывает это.
Спасибо!