Neue Zeile einfügen in UITableView beim drücken der ersten Zeile
Ich bin neu in der iOS-Entwicklung. Daher wäre es schön, wenn Ihre Antwort könnte so detailliert wie möglich. Mein problem ist Folgendes: ich möchte die Zelle in meinem zweiten Abschnitt sagen immer "add new row". Wenn Sie auf diese Zelle eine neue Zeile Hinzugefügt werden soll an der Spitze. Mein Aktueller code ist
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//Return the number of rows in the section.
if (section == 0) {
return 1;
}
else {
return [entries count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"Cell";
static NSString *todayTableIdentifier = @"Notification";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
UITableViewCell *todaycell = [tableView dequeueReusableCellWithIdentifier:todayTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSString *entry;
if (indexPath.section == 0) {
entry = [notifications objectAtIndex:indexPath.row];
cell = todaycell;
} else {
entry = [entries objectAtIndex:indexPath.row];
}
cell.textLabel.text = entry;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int selectedRow = indexPath.row;
NSLog(@"row pressed %d", selectedRow);
NSArray *paths = [NSArray arrayWithObject:
[NSIndexPath indexPathForRow:2 inSection:2]];
if (selectedRow == 0)
{ [entries addObject:@"a new Entry"];
[[self tableView] insertRowsAtIndexPaths:paths
withRowAnimation:UITableViewRowAnimationTop];
}
else {
[[self tableView] deleteRowsAtIndexPaths:paths
withRowAnimation:UITableViewRowAnimationTop];
}
}
Leider meine app stürzt immer ab.
2013-04-11 16:52:39.717 MyStore[12924:c07] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:861
2013-04-11 16:52:39.718 MyStore[12924:c07] *** Terminating app due to uncaught exception ' NSInternalInconsistencyException', reason: 'attempt to delete row 2 from section 2, but there are only 2 sections before the update'
*** First throw call stack:
(0x1faa012 0x13e7e7e 0x1fa9e78 0xb6d665 0xbb43c 0xca1f6 0xca271 0x5c6b 0xcb285 0xcb4ed 0xad55b3 0x1f69376 0x1f68e06 0x1f50a82 0x1f4ff44 0x1f4fe1b 0x1f047e3 0x1f04668 0x1bffc 0x21bd 0x20e5 0x1)
libc++abi.dylib: terminate called throwing an exception
InformationsquelleAutor Korg | 2013-04-11
Du musst angemeldet sein, um einen Kommentar abzugeben.
Dass das normal ist, neben dem hinzufügen einer neuen Zeile (optisch), müssen Sie die Datenquelle aktualisieren (die Benachrichtigungen array in diesem Fall). Die UI ist nur die Visualisierung Ihrer Daten-Quelle. Also:
Und wenn Sie Sie löschen:
Erklären Benachrichtigungen als
NSMutableArray
.Danke, aber jetzt meine Konsole sagt: Versuch zum einfügen der Zeile 3 in Abschnitt 2, aber es gibt nur 2 Abschnitte nach dem update. Ich hab den 2 Teil. Ich möchte einfügen, meine Zeile in der zweiten. Die zweite enthält 2 Zeilen.
Sorry, nicht verstehen...
Es sollte
[NSIndexPath indexPathForRow:2 inSection:1]];
Zwei Abschnitte.... Index 0 und 1...InformationsquelleAutor Rui Peres