В моей самой верхней ячейке в моей таблице есть текстовое поле, которое используется для добавления нового имени в табличное представление. У меня так, что добавляемое имя оживляет снизу текстовое поле. Я хотел бы, чтобы каждая новая ячейка имела другой цвет фона, чем существующие ячейки.
Я пробовал
[self.tableView selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:nil];
но это ничего не делает. Я думаю, что это может быть потому, что новая ячейка находится в переходе, и у нее еще нет индекса. Не уверен, но я знаю, что это не работает. Любые идеи или примеры из других стран?
--- ОБНОВЛЕНО ---
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TextFieldCell *customCell = (TextFieldCell *)[tableView dequeueReusableCellWithIdentifier:@"TextCellID"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (indexPath.row == 0) {
if (customCell == nil) {
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"TextFieldCell" owner:nil options:nil];
for (id currentObject in nibObjects) {
if ([currentObject isKindOfClass:[TextFieldCell class]])
customCell = (TextFieldCell *)currentObject;
}
}
customCell.nameTextField.delegate = self;
cell = customCell;
}
else {
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
cell.textLabel.text = [[self.peopleArray objectAtIndex:indexPath.row-1] name];
}
return cell;
}
Куда я добавляю имя человека:
- (void)textFieldDidEndEditing:(UITextField *)textField {
// If the textfield isn't blank, go ahead and add the person
if ([textField.text length] != 0) {
// Create new person
Person *addedPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:[self context]];
// Set person's information
[addedPerson setName:[textField.text capitalizedString]];
// Add the person to the beginning of the datasource array
[self.peopleArray insertObject:addedPerson atIndex:0];
// Get the person's name from the textfield so that I can add their name later
TextFieldCell *textCell = [[TextFieldCell alloc] init];
NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];
textCell = (TextFieldCell *)[self.tableView cellForRowAtIndexPath:path];
// Set the label below the textfield to the text from the textfield above
NSString *label = [NSString stringWithFormat:@"%@ was added", [textField text]];
textCell.addedNameLabel.text = label;
// Clear textfield for next entry
textField.text = @"";
// Animation to insert the new person at the top of the tableview
NSIndexPath *pathInsert = [NSIndexPath indexPathForRow:1 inSection:0];
NSArray *indexArray = [NSArray arrayWithObjects:pathInsert,nil];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
}
}