![](/img/trans.png)
[英]TableView removes cells when I ask it to add more cells when scrolled to the bottom. Why?
[英]Add cells to my tableView when I make the scroll on it and finish at the bottom of it
我在UIViewController中有一個tableView。 當我加載viewController時,正在加載帶有3個單元格的tableView。 每當我在tableView上制作滾動並在其底部完成時,我希望它被添加到它(其他)3個單元格中。 facebook應用程序怎么樣! 請幫幫我! 這是我在代碼中遇到的(不添加單元格):
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//I created an int flag because this method (as soon as it initialize the
//table) is called 2 times (because when it load the viewController, is called
// the method that populates the arrays that contain the details to be shown in
// the cells and reload the tableView)
if (_flagCells == 0 || _flagCells == 1) {
_flagCells ++;
return;
}
if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
//phpClass is a class that contain scripts used to connect my app to script php for mySQL DB
//objsRequest is a method of phpClass that receive a query for input and return an array of //results of that query. cellsLimit represent the cells to be shown (Once every 3 to 3) and //numberOfDequeuedCells represent the number of cells to be added (every 3) that is SELECT .. //FROM .. LIMIT 0,3 .... LIMIT 3,3 .... LIMIT 6,3 ....
[_arrID addObjectsFromArray:[_phpClass objsRequest:[NSString stringWithFormat:@"SELECT ID FROM mii LIMIT %d,%d",_cellsLimit,_numberOfDequeuedCells]]];
for (int i = (int)[_arrID count]-3; i < [_arrID count]; i++) {
//objRequest is a method of phpClass that receive a query for input and return a string that
//represent the result of that query
[_arrNames addObject:[_phpClass objRequest:[NSString stringWithFormat:@"SELECT Name FROM mii WHERE ID = %@",_arrID[i]]]];
[_arrGen addObject:[_script objRequest:[NSString stringWithFormat:@"SELECT Gen FROM mii WHERE ID = %@",_arrID[i]]]];
//getImg is a method of phpClass that receive the user ID for input and return an image
//representing the user image
UIImage *img = [_phpClass getImg:_arrID[i]];
[_arrImgs insertObject:img atIndex:i];
//Here is the problem because the rows doesn't be added to my tableView
[_tableView beginUpdates];
[_tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
}
_cellsLimit +=3;
[_tableView endUpdates];
}
}
我前段時間做過這件事。 如果我沒記錯,那么我只是將其他數據加載到我用作表中數據容器的數組中。 加載數據后,我重繪表格。 而已。
我認為你迷失只是因為你試圖讓它更復雜。
至於你的評論:
[_tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.001];
要么
[_tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
兩者都應該在另一個線程中執行reloadData,因此不應該導致循環。 但是,即使這樣,您也應該在表格結束之前添加新數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.