2015-10-21 25 views
0

我有一个最初有20个元素的tableview数据源。之后数据源更新更多的元素。为了更新tableview我称之为tableview重载。当它被调用时,滚动位置突然改变。有没有一种方法可以阻止它滚动,并使它保持在我正在看的地方?当数据源添加更多元素和可变单元格高度时,UITableView会滚动

所以我发现了为什么滚动发生。我已经实现了heightForRowAtIndex,当我评论它时,表视图不会自动滚动。但我仍然没有找到这个解决方案

+0

显示您的代码。 –

+0

显示代码...... –

+0

如果您发布了代码,以便可以检查您实际发生错误的位置 – z22

回答

0

我觉得你说错了..我试着按你说的做,我创建了一个20个元素的表视图,添加了一个按钮,添加了20个元素,并称为“ reloadData”。结果是表格没有移动或滚动。新数据被添加到表格的末尾。我滚动到桌子的尽头后,我看到了它。

- (void)viewDidLoad { 
[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 


self.arr = [NSMutableArray new]; 

for (self.i=0; self.i<10; self.i++) { 
    [self.arr addObject:[NSString stringWithFormat:@"%d",self.i]]; 
} 

[self.tableView setDelegate:self]; 
[self.tableView setDataSource:self]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
return self.arr.count; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

[cell.textLabel setText:self.arr[indexPath.row]]; 

return cell; 
} 
- (IBAction)addMoreDataToArr:(id)sender { 
int oldI = self.i; 
for (self.i = self.i; self.i<oldI+10; self.i++) { 
    [self.arr addObject:[NSString stringWithFormat:@"%d",self.i]]; 
} 

[self.tableView reloadData]; 
} 

- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

供参考:这是非常糟糕的代码:)它只是为了POC!

相关问题