У меня есть элемент управления TableView, который я секционировал/сгруппировал. Однако теперь он нарушил код, который определяет элемент, который я выбрал в таблице.TableView возвращает null (0) при выборе строки
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
/*
When a row is selected, set the detail view controller's detail item to the item associated with the selected row.
*/
//detailViewController.detailItem = [NSString stringWithFormat:@"Row %d", indexPath.row];
if (_delegate != nil) {
Condition *condition = (Condition *) [_conditions objectAtIndex:indexPath.row];
[_delegate conditionSelectionChanged:condition];
}
}
В настоящее время у моих выборочных данных имеется 6 разделов, первый из которых содержит два элемента со всем остальным, содержащим один. Если я выбираю второй элемент в первом разделе, представление сведений обновляется отлично, любой другой выбор всегда показывает первый элемент в первом разделе (пункт 0).
Я предполагаю, что это потому, что indexPath.row возвращает 0 для других разделов, поскольку это первый элемент в этом разделе, который затем вызывает выбор первого объекта из моего массива.
Как исправить мой код, чтобы убедиться, что выбран правильный элемент? Все остальное отлично работает, кроме этого.
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CellIdentifier";
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}
//get the letter in the current section
NSString *alphabet = [conditionIndex objectAtIndex:[indexPath section]];
//get all conditions beginning with the letter
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name beginswith[c] %@", alphabet];
NSArray *conditionsGrouped = [_conditions filteredArrayUsingPredicate:predicate];
if ([conditionsGrouped count] > 0) {
Condition *condition = [conditionsGrouped objectAtIndex:indexPath.row];
cell.textLabel.text = condition.name;
}
return cell;
}
Спасибо за этот Чиприан. Можете ли вы дать мне некоторую идею, чтобы начать или указать мне на учебник или образец, я изучаю это, когда я иду? – sparkymark75
Нет учебника для вашей конкретной необходимости. Как вы реализовали cellForRowAtIndexPath? –
Я добавил этот код к моему вопросу выше. Благодарю. Это также вызывает проблемы при поиске, так как возвращаются только элементы из первого раздела. – sparkymark75