简体   繁体   English

我的UITableView中无法加载40个以上的UITableViewCells

[英]I can't load more than 40 UITableViewCells in my UITableView

I am parsing XML that gets the first 25 items in my MySQL database using PHP - LIMIT and GET. 我正在解析使用PHP-LIMIT和GET来获取MySQL数据库中前25个项目的XML。 When I click on the "Load More" cell that I append to the bottom of my tableview, it successfully gets the next 25 items, but only loads the first 40 and leaves off the last 10. Each time I click on the "Load more" cell it add 25 to my range (ie 0-25,25-50), but it seems that my range caps at 65 and the display caps at 40. 当我单击添加到表格视图底部的“加载更多”单元格时,它成功获取了下25个项目,但仅加载了前40个项目,而保留了最后10个项目。每次单击“加载更多”单元格将其添加25到我的范围(即0-25,25-50),但似乎我的范围上限为65,显示上限为40。

Here is my load more function thats not working: 这是我的更多加载功能不起作用:

-(void) getNewRange{
    int currentRange = [allItems count];
    int newRange = currentRange + 25;

    if(newRange > [xmlParser total]){
        NSLog(@"evaluating as greater than the total, which is 837");
        newRange = [xmlParser total];
    }

    NSString *range = [[NSString alloc] initWithFormat:@"?range=%d&range2=%d",currentRange,newRange];
    NSString *newUrl =[[NSString alloc] initWithFormat:@"http://localhost/fetchAllTitles.php%@",range];

    XMLParser *tempParser = [[XMLParser alloc] loadXMLByURL:newUrl];
    [allItems addObjectsFromArray:[tempParser people]];
    NSMutableArray *newCells = [[NSMutableArray alloc] initWithCapacity:25];

    for(int i=currentRange;i<newRange;i++){
        NSLog(@"%d",i);
         NSIndexPath *indexpath=[NSIndexPath indexPathForRow:i inSection:0];
        [newCells addObject:indexpath];
    }

    NSLog(@"%@",newUrl);

    [self.tableView insertRowsAtIndexPaths:newCells withRowAnimation:UITableViewRowAnimationAutomatic];
}

I'm getting closer, but I get this new error: 我越来越近了,但是出现了这个新错误:

*** Assertion failure in -[_UITableViewUpdateSupport _computeRowUpdates], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableViewSupport.m:386

Read up on how you can Reuse your table view's cells . 阅读有关如何重用表格视图的单元格的内容

Your data does not need to be 'owned' by the cell. 您的数据不需要由单元“拥有”。

UITableView isn't a class to contain your data, and you shouldn't try to directly micromanage what cells it displays. UITableView不是包含您的数据的类,并且您不应该尝试直接微管理其显示的单元格。 As another poster stated, read up on how to use it. 正如另一位张贴者所说,请阅读如何使用它。 What you should do is: 您应该做的是:

-(void)loadNewData
{
    NSIndexPath *index;
    XMLParser *tempParser = [[XMLParser alloc] loadXMLByURL:newUrl];
    NSArray *people=[tempParser people];
    for(id *person in people)
    {
        [self.dataArray addObject:person];
        indexPath=[NSIndexPath indexPathForRow:[self.dataArray indexForObject:person] inSection:0];
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{ 
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [AnArray count];//use whatever array stores your data
}


//If you've subclassed the cell, adjust appropriately.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    //Customize the cell
    return cell;
}

The table view will take care of all the logic involved in displaying the cells, if you let it. 如果允许的话,表格视图将处理显示单元格所涉及的所有逻辑。 This way, you only have a limited number of cells taking up memory at any given time, and you don't have to handle that -- the table view automagically handles reusing the cells, and knowing how many are needed as a buffer before / after. 这样,您在任何给定时间只有有限数量的单元占用内存,而不必处理-表格视图自动地处理单元的重用,并知道在/之前需要多少个缓冲区后。

you should not set numberOfRowsInSection inside your method. 您不应在方法内部设置numberOfRowsInSection。 The number of rows should get returned from the tableView's datasource method - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section . 行数应从tableView的数据源方法- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section Just return [allItems count] there. 只需在[allItems count]返回[allItems count]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我无法在自定义UITableViewCells周围出现阴影 - I can't get shadows to appear around my custom UITableViewCells 我无法从JSON获取数组以加载到UITableView中 - I can't get my array from JSON to load into the UITableView 您是否可以缓存不仅仅是显示的UITableViewCells - Can you cache more UITableViewCells than just those on display UITableView 不显示 UITableViewCells - UITableView doesn't display UITableViewCells 如何在iOS分组的UITableView中获取Rectangle UITableViewCells - How can I get Rectangle UITableViewCells in an iOS grouped UITableView 如何在没有UITableViewController的情况下在UITableView中显示原型UITableViewCells? - How can I show prototype UITableViewCells in a UITableView without a UITableViewController? 如何将我的UITableViewCells存储在NSMutableArray中? - How can I store my UITableViewCells in a NSMutableArray? 如何在一个UIViewController中添加多个UITableView? - How can I add more than one UITableView in a UIViewController? 我在UITableView中混合了静态和动态UITableViewCells,并在numberOfRowsInSection中得到了错误的值 - I mixed static and dynamic UITableViewCells in my UITableView and got wrong values in numberOfRowsInSection 如何从UITableView的UITableViewCells中的UITextFields中获取文本并将其放入数组中(Swift 3)? - How can I take the text from UITextFields in UITableViewCells in a UITableView and put them into an array (Swift 3)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM