UITableView在保持已经被提取的同时插入更多行

时间:2012-12-28 11:15:54

标签: iphone ios uitableview uiscrollview

以下是我用来获取和解析Feed的不同页面的功能。

- (void)getFeed:(NSInteger) pageNumber
{
    collectedData = [[NSMutableData alloc] init];
    NSString *urlStr =[NSString stringWithFormat:@"http://sitename.com/feed/?paged=%d", pageNumber];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
}

当用户向下滚动到tableview的底部时,我使用以下函数访问feed的第二页,依此类推:递增计数器:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    float endScrolling = scrollView.contentOffset.y + scrollView.frame.size.height;
    if (endScrolling >= scrollView.contentSize.height)
    {
        counter ++;
        [self getFeed:counter];
    }
}

问题是,当它不是在已经获取的项目下面加载它时,它会重新加载tableview以显示新的页面项目,而旧的页面项目会消失。我想在现有页面下面加载新的页面项目以进行无限滚动。我怎么能这样做?

7 个答案:

答案 0 :(得分:2)

回答你自己的问题感觉不好,但是从另一个论坛获得帮助,我能够找到答案。

tableview控制器与可变数组不同步,所以我创建了另一个并附加了两个数组。

Step1:我创建了一个属性

@property (nonatomic, readonly) NSMutableArray *moreItems;

步骤2:在调用fetchEntries

之前,在ViewDidLoad中初始化数组
moreItems = [[NSMutableArray alloc] init];

Step3:在connectionDidFinishLoading中,我将新数组'moreItems'附加到前一个数组'items'

[moreItems addObjectsFromArray:[channel items]];

步骤4:更改了numberOfRowsInSection中的数据源

return [[channel items] count];

到新数组

return [moreItems count];

然后在cellForRowAtIndexPath中使用新数组

RSSItem *item = [moreItems objectAtIndex:[indexPath row]];

答案 1 :(得分:1)

每当您的数组添加一个新对象时,请调用此

-(void)insertRowInTableView
 {
   int row = [YourDataSourceArray count];
   NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
   NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
   [YourTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
 }

此处 YourDataSourceArray 是您从中设置tableview行数/数据的数组。 YourTableView 是您的tableView对象名称。

答案 2 :(得分:1)

您应该使用insertRowsAtIndexPaths: withRowAnimation:的{​​{1}}方法。

您必须注意的一件事是,当您使用此方法时,UITableView重新调整的数字行应等于tableView:numberOfRowsInSection

答案 3 :(得分:1)

完成检索下一页的新Feed后,获取数组中的对象。 将此数组中的这些对象添加到数据源数组中。 现在将行插入表中,如下所示:

[yourTable beginUpdates];
[yourTable insertRowsAtIndexPaths:indexPathsToInsert
                 withRowAnimation:UITableViewRowAnimationBottom];
[yourTable endUpdates];

其中" indexPathsToInsert"是NSIndexPath对象的数组,从表的最后一行开始,包含新对象的indexPath。

希望这有帮助。

答案 4 :(得分:1)

嗨,

创建一个for循环,其起始索引是(数据源中的对象数)和要添加的对象数(count),创建索引路径数组并调用insertRowsAtIndexpaths:withRowAnimation。我希望代码可以帮助你。

    NSInteger statingIndex = [self.tableViewDatasource count];
    NSInteger noOfObjects = //Get the no.of objects count from getFeed method.
    NSMutableArray *indexPaths = [[NSMutableArray alloc] init];

    for (NSInteger index = statingIndex; index < statingIndex+noOfObjects; index++) {

        [_objects addObject:]; // add the object from getFeed method.
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
        [indexPaths addObject:indexPath];
        [indexPath release];

    }

    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];

答案 5 :(得分:0)

这可能是因为当您从Web服务获取数据时,您要将其添加到用于填充表的数组中,这样您将只有新数据,您需要做的就是必须将新数据附加到数组(Make it Mutable Array并附加新数据),然后使用以下行

[your_Table reloadData];

希望这会对你有所帮助。

答案 6 :(得分:0)

不要使用向下滚动加载,而是更好地使用tableview的最后一行作为&#34;加载更多项目&#34;当用户选择该行时,获取新的Feed并重新加载tableview。

而且我对你的代码有一个疑问,每次使用getfeed方法,你都在创建数组 collectedData。我怀疑你没有将这些数据附加到表数据源数据,这可能导致只显示tableview中的最近的feed数据。请检查