簡體   English   中英

通過平滑滾動在UITableView中插入最后一行

[英]Insert last row in UITableView with smooth scrolling

我正在嘗試創建一個像聊天機器人這樣的應用程序,我使用了帶有自定義單元格的UITableView來滿足我的需求。 每當添加新消息時,我都會創建並插入新行,然后滾動到UITableView的底部。 一切正常,直到某一點,但是當單元格的高度發生變化(我有兩種不同類型的單元格)時,動畫是混亂的,它無法平滑滾動到末尾並且整個UITableView閃爍,這不是良好的用戶體驗。 我嘗試了幾種方法:

1-將數據添加到數據源數組並重新加載UITableView ,然后滾動到底部。

2-使用insertRowsAtIndexPaths然后滾動到底部。

他們兩個都有相同的滾動問題。 我使用了scrollToRowAtIndexPath到達UITableView的底部

我已上傳了一個演示應用程序的代碼,該代碼在此處表示模擬相同的問題因此很容易理解。 是該問題的視頻。 任何幫助都非常感謝。

此問題可能不會在模擬器上發生,請在設備上運行演示項目。

在閱讀所有評論並在聊天中進行討論之后,我注意到這在iPhone 5C(10.3.3)上正在發生。 我在iPhone 5S(11.3)上運行了演示,但沒有出現此問題。 不知道這是否與操作系統有關。

重新加載整個tableview會導致混亂的滾動,而不是在最后一個位置插入行。

在您的操作方法中進行以下更改

- (IBAction)btnLargeCellClicked:(id)sender {    // To add large green colored row.

    /************ This is the FIRST approach. ***********/
    [arrHeight addObject:@"100"];
    [arrData addObject:@"1"];
    NSIndexPath* ip = [NSIndexPath indexPathForRow:[_myTable numberOfRowsInSection:0] inSection:0];
    [_myTable insertRowsAtIndexPaths:@[ip] withRowAnimation:UITableViewRowAnimationFade];
    [self scrollTableviewToLastRow];


    /************ This is the SECOND approach. ***********/
//    [arrHeight addObject:@"100"];
//    [_myTable beginUpdates];
//    NSIndexPath *row1 = [NSIndexPath indexPathForRow:arrData.count inSection:0];
//    [arrData insertObject:@"1" atIndex:arrData.count];
//    [_myTable insertRowsAtIndexPaths:[NSArray arrayWithObjects:row1, nil] withRowAnimation:UITableViewRowAnimationFade];
//    [_myTable endUpdates];
//    [self scrollTableviewToLastRow];
}

- (IBAction)btnClicked:(id)sender {     // To add small red colored row.
    /************ This is the FIRST approach. ***********/
    [arrHeight addObject:@"50"];
    [arrData addObject:@"1"];
    NSIndexPath* ip = [NSIndexPath indexPathForRow:[_myTable numberOfRowsInSection:0] inSection:0];
    [_myTable insertRowsAtIndexPaths:@[ip] withRowAnimation:UITableViewRowAnimationFade];
    [self scrollTableviewToLastRow];


    /************ This is the SECOND approach. ***********/
//    [arrHeight addObject:@"50"];
//    [_myTable beginUpdates];
//    NSIndexPath *row1 = [NSIndexPath indexPathForRow:arrData.count inSection:0];
//    [arrData insertObject:@"1" atIndex:arrData.count];
//    [_myTable insertRowsAtIndexPaths:[NSArray arrayWithObjects:row1, nil] withRowAnimation:UITableViewRowAnimationFade];
//    [_myTable endUpdates];
//    [self scrollTableviewToLastRow];

}

希望這可以幫助 :)

reloads cells every time, that makes animation more busy every additional cell. 看到您的源代碼之后,可以說每次都重新加載單元格,這會使動畫在每個其他單元格上更加忙碌。 為了您的目標,您應該使用另一個系統:

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates]; 

at the end too 而且,也許最后您也需要

您將需要此,更改如下的第二種方法

[arrHeight addObject:@"50"];
//    [_myTable beginUpdates];
    NSIndexPath *row1 = [NSIndexPath indexPathForRow:arrData.count inSection:0];
    [arrData insertObject:@"1" atIndex:arrData.count];
    [_myTable insertRowsAtIndexPaths:[NSArray arrayWithObjects:row1, nil] withRowAnimation:UITableViewRowAnimationNone];
    [_myTable reloadRowsAtIndexPaths:@[row1] withRowAnimation:UITableViewRowAnimationNone];

//    [_myTable endUpdates];
    [self scrollTableviewToLastRow];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM