我想在一个 TableView 中的最后一行的前面同时插入很多行,但是它在最后一行前面加了一行,最后又加了两行。如何弄清楚?请帮助我,提前谢谢你。 !
- (void)morePicture:(id)sender{
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
for (int i=0; i<3; i++) {
NSString *s = [[NSString alloc] initWithFormat:@"%d",i];
[photos addObject:s];
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:0];
[indexPaths addObject:indexpath];
}
[table beginUpdates];
[table insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
[table endUpdates];
[table reloadData];
}
最佳答案
- (void)morePicture:(id)sender {
// See how many rows there are already:
NSUInteger rowCount = [table numberOfRowsInSection:0]
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
for (int i=0; i<3; i++) {
NSString *s = [[NSString alloc] initWithFormat:@"%d",i];
[photos addObject:s];
// The new index path is the original number of rows plus i - 1 to leave the last row where it is.
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i+rowCount - 1 inSection:0];
[indexPaths addObject:indexpath];
}
[table beginUpdates];
[table insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
[table endUpdates];
[table reloadData];
}
关于ios - 如何在tableview中同时插入多行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9798755/