iOS:重新加载后显示表视图的最后一个单元格

时间:2016-07-22 05:52:22

标签: ios uitableview

我有表视图,其中包含一些部分和行。 我希望在加载或重新加载视图时将上一部分中的最后一个单元格设置为可见。

我正在使用以下代码,但我的应用在第二行崩溃了:

  NSIndexPath* lastIndexPath = [NSIndexPath indexPathForRow:[arrmChatLine count] inSection:[arrSectionTitles count]];
  [tablevMessageChat scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];

请建议我解决方案或告诉我我做错了什么。 谢谢。

3 个答案:

答案 0 :(得分:0)

检查numberOfRowsInSection委托和numberOfSectionsInTableView,以便将它们设置为所需值。

在调用scrollToRowAtIndexPath之前重新加载TableView数据。

答案 1 :(得分:0)

以下是解决方案:

// First figure out how many sections there are
int lastSectionIndex = [tablevMessageChat numberOfSections]-1;

// Then grab the number of rows in the last section
int lastRowIndex = [tablevMessageChat numberOfRowsInSection:lastSectionIndex]-1;

// Now just construct the index path
NSIndexPath * pathToLastRow = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];

// Make the last row visible
[tablevMessageChat scrollToRowAtIndexPath:pathToLastRow atScrollPosition:UITableViewScrollPositionBottom animated:NO];

答案 2 :(得分:0)

替换为:

[NSIndexPath indexPathForRow:[arrmChatLine count] - 1 inSection:[arrSectionTitles count] - 1]; 

您的当机问题将得到解决...