我[self.messageList scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
在 cellForRowAtIndexPath 中使用它,它工作正常,当在表数组中添加一行时,它会自动向上滚动,但问题是当我尝试查看最上面的行时,它会自动再次出现最后一行。而且我无法看到我的所有行。请让我知道我在哪里使用这个或任何其他方法来做到这一点。
问问题
1315 次
3 回答
1
cellForRowAtIndexPath
不会为您拥有的所有单元格调用方法。仅对 TableView 中的可见单元格调用它。当您滚动时UITableView
,它会询问UITableView
您要使用可重复使用的单元格或添加到新单元格。通常我们使用可重复使用的单元格,所以它是使用第一个单元格并在单元格末尾添加。它不是在最后创建另一个新单元格。因此,当您滚动它时UITableView
,它不会滚动到单元格末尾,它indexPath
类似于indexPath
第一个单元格。所以你不能将你的UITableView
at 滚动到 Down。
使用以下代码可能对您有所帮助.... :)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
[self.tblView scrollToRowAtIndexPath:indexPath atScrollPosition: UITableViewScrollPositionTop animated: YES];
// your code
}
// your code
return cell;
}
于 2012-10-05T07:31:34.410 回答
0
cellForRowAtIndexPath
是一种创建要查看的单元格的方法。当您滚动查看单元格时调用此方法。因此,每次调用该 方法的[self.messageList scrollToRowAtIndexPath:indexPath
方法(在内部调用cellForRow
)也被调用,并且表格向下滚动到该行! !
于 2012-10-05T07:14:22.983 回答
0
如果您在单元格中为索引路径处的行编写此代码,那么每次您尝试手动滚动时都会调用它,因为会调用此消息来绘制您的单元格,并且您将被定向到您指定的行。尝试在 IF--Else 块中检查您的索引路径,并仅在发生特定操作时滚动。
于 2012-10-05T07:10:40.590 回答