1.在TableView的numberOfRowsInSection方法中設置contentOffSet- (NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{
? ? if (self.isScrollBottom) { //只在初始化的時候執(zhí)行
? ? ? ? dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0005 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
?? ? ? ? ? ? ? ? if(self.messages.count>0) {
? ? ? ? ? ? ? ? ? ? NSIndexPath *indexPath = [NSIndexPath indexPathForRow:([self.messageTable numberOfRowsInSection:0]-1) inSection:0];
? ? ? ? ? ? ? ? ? ? [self.messageTable scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
?? ? ? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? return self.messages.count;
}
2.這種方法要加一個延時,不然會造成死循環(huán)华弓。
在viewDidAppear中將屬性改為NO
- (void)viewDidAppear:(BOOL)animated
{
? ? [super viewDidAppear:YES];
? ? self.isScrollBottom = NO;
}
3.解決向上拉卡頓 緩存行高
- (CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:(NSIndexPath*)indexPath{
? ? CGFloat height;
? ? if(self.heightArray.count> indexPath.row) {
? ? ? ? // 如果有緩存的高度载迄,取出緩存高度
? ? ? ? height = [self.heightArray[indexPath.row]floatValue];
? ? }else{
? ? ? ? // 無緩存高度,計算高度,并加入數(shù)組
? ? ? ? XJMessage*currentMsg =self.messages[indexPath.row];
? ? ? ? height = currentMsg.messageHeight;
? ? ? ? [self.heightArray addObject:[NSNumber numberWithDouble:height]];
? ? }
? ? returnheight;
}