Iphone insertRowsAtIndexPaths不调用cellForRowAtIndexPath
我创建了示例tableview应用程序,在tableview上方有一个add按钮,当用户按下add按钮时,我们只想向tableview添加行 我正在写这样的代码Iphone insertRowsAtIndexPaths不调用cellForRowAtIndexPath,iphone,Iphone,我创建了示例tableview应用程序,在tableview上方有一个add按钮,当用户按下add按钮时,我们只想向tableview添加行 我正在写这样的代码 - (void)viewDidLoad { isEditing = NO; Mutarray = [[NSMutableArray alloc]init]; [super viewDidLoad]; } - (NSInteger)numberOfSectionsInTableView:(UITableView
- (void)viewDidLoad {
isEditing = NO;
Mutarray = [[NSMutableArray alloc]init];
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
if (isEditing)
return [Mutarray count];
else
return 0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
[TableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
NSLog(@"Row = %d", indexPath.row);
// Configure the cell...
cell.textLabel.text = [Mutarray objectAtIndex:indexPath.row];
return cell;
}
//When add button pressed
-(IBAction)Add:(id)sender
{
isEditing = YES;
[Mutarray addObject:[NSString stringWithFormat:@"%d",[Mutarray count]]];
NSArray *insertIndexPaths = [NSArray arrayWithObjects:
[NSIndexPath indexPathForRow:[Mutarray count]-1 inSection:0],
nil];
[self.TableView beginUpdates];
[self.TableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
[self.TableView endUpdates];
}
这段代码很好用。但问题是我的tableview高度是418,它只显示10行可见。因此,当11行被添加时,它被添加到tableview中,但没有调用此CellForRowatineXpath函数,因此我无法自动滚动页面。。。它调用cellForRowAtIndexPath函数的前10行
所以我怀疑的是为什么cellForRowAtIndexPath函数只调用可见行?
那么如何自动滚动我的tableview
所以我怀疑的是为什么
cellForRowAtIndexPath函数仅限
调用可见行
出于优化的原因,情况就是如此。若表视图为其所有行创建了单元格,则可能会显著降低性能。与此相反,表视图只为可见的行创建单元格(为了确保平滑滚动,可能会创建更多的单元格),然后为可见的行重用这些单元格-因此,实际上,您可以仅使用10个单元格对象来显示数百行-这是一个巨大的节省
那我怎么才能自动滚动我的页面呢
桌面视图
您可以在add
方法中添加行后立即滚动:
...
[table endUpdates];
[table scrollToRowAtIndexPath:[insertIndexPaths objectAtIndex:0]
atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
p.S.按照惯例,objective-c中的方法和变量名以小写字母开头,最好遵循该准则
所以我怀疑的是为什么
cellForRowAtIndexPath函数仅限
调用可见行
出于优化的原因,情况就是如此。若表视图为其所有行创建了单元格,则可能会显著降低性能。与此相反,表视图只为可见的行创建单元格(为了确保平滑滚动,可能会创建更多的单元格),然后为可见的行重用这些单元格-因此,实际上,您可以仅使用10个单元格对象来显示数百行-这是一个巨大的节省
那我怎么才能自动滚动我的页面呢
桌面视图
您可以在add
方法中添加行后立即滚动:
...
[table endUpdates];
[table scrollToRowAtIndexPath:[insertIndexPaths objectAtIndex:0]
atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
p.S.按照惯例,objective-c中的方法和变量名以小写开头,遵循这一指导原则更好。谢谢Vladimir。看起来像是格雷特·汉克斯·弗拉基米尔。看起来很棒