在uitableview的末尾添加新行

时间:2014-03-04 13:23:42

标签: ios uitableview

我试图在用户滚动到视图结尾后添加新闻行。

前10行来自第一页提取,然后我重新加载我的表。

这是第一页代码:

SubViewController *svc =[self.storyboard instantiateViewControllerWithIdentifier:@"Subview"];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),
                   ^{
                       ParseOperation *p = [[ParseOperation alloc]init];

                       p.code = selection_code.text;
                       [p main];
                   dispatch_async(dispatch_get_main_queue(), ^(void)
                                       {


                                          svc.entries = p.appRecordList;
                                          [svc.tableView reloadData];

                                      });
                   });

    [svc setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
    [self.navigationController pushViewController:svc animated:YES];

对于第二页,我发现了滚动项的结尾:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
......

   if ((unsigned long)indexPath.row == [self.entries count] - 1){
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),
                       ^{
                           ParseOperation *p = [[ParseOperation alloc]init];
                           AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];


                           p.lastid = appRecord.ids;
                           [p main];
                           dispatch_async(dispatch_get_main_queue(), ^(void)
                                          {

                                              SubCategoryViewController *svc =[self.storyboard instantiateViewControllerWithIdentifier:@"SubView"];

                                               svc.entries = p.appRecordList;
                                              [svc.tableView reloadData];

                                          });
                       });

    }
    return cell;

提取的解析词和获取的新rss项。但桌子不爽。

编辑(添加numberOfRowInSection代码)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger count = [self.entries count];

    // if there's no data yet, return enough rows to fill the screen
    if (count == 0)
    {
        return kCustomRowCount;
    }
    return count;
}

3 个答案:

答案 0 :(得分:1)

您可以使用insertAtIndexPath方法为UITableView在tableView的最后一个对象下面插入新行。

  -(void)insertRowInTable:(NSArray*)DictOfWebData{
    NSMutableArray *mutArrOfIndexPath=[[NSMutableArray alloc]init];
        int count=arrOfTbleView.count;
        for (NSDictionary *dict in [DictOfWebData objectForKey:@"link"]) {
            [arrOfTbleView addObject:dict];
            [mutArrOfIndexPath addObject:[NSIndexPath indexPathForRow:count inSection:0]];
            count++;

        }
        [_tblVwOFWebData beginUpdates];
        [_tblVwOFWebData insertRowsAtIndexPaths:mutArrOfIndexPath withRowAnimation:UITableViewRowAnimationFade];
        [_tblVwOFWebData endUpdates];
       }

从scrollViewDidEndDragging方法调用此方法,在调用此方法之前,您必须从webservice获取数据..

我认为这会对你有帮助..

答案 1 :(得分:1)

代码对我来说没有任何意义,看起来你有两个不同的类,SubViewControllerSubCategoryViewController,其中一个包含tableView?您正使用标识符“SubView”实例化它们。在我看来,你可以删除整个SubCategoryViewController类,并且只使用SubViewController。我可能会误解他们的目的。

无论哪种方式,在cellForRowAtIndexPath - 方法中,您都要实例化一个新的空视图控制器。 从cellForRow..内部查看您自己的代码:

SubCategoryViewController *svc =[self.storyboard 
                         instantiateViewControllerWithIdentifier:@"SubView"];
svc.entries = p.appRecordList;                
[svc.tableView reloadData];

此代码的作用是实例化一个新的SubCategoryViewController,也称为'svc'。它与您当前的视图控制器无关。它是另一个,有自己的表视图。您正在将新数据推送到新的不可见视图控制器,而您永远不会使用它。在[.. reloadData]之后,您的新视图控制器将被删除。

在我看来,您应该使用[self ..]而不是新的,无用的svc

代替上面的代码,试试这个(并删除subCategory-instantiation):

self.entries = p.appRecordList;                
[self.tableView reloadData];

请注意,此代码会将当前self.entries内的数据替换为新数据。如果您的新数据是另一个页面,那么您应该首先附加数据,否则表中的数据总是10行。 我不知道对象entries是什么类型的(您的数据源),所以我无法告诉您如何附加数据。如果它是NSArray,您可以执行以下操作:

NSArray *temp = [self.entries arrayByAddingObjectsFromArray:p.appRecordList];
self.entries = temp;

如果是NSMutableArray,您可以转到[self.entries addObjectsFromArray:p.appRecordList];。如果你正在使用别的东西,我相信它们也有附加的方法。

注意:如果我对SubView..SubCategoryView..的错误,请评论两者之间的区别,我会试着弄明白。

答案 2 :(得分:1)

试试这个希望,这将有助于您的要求。当您第一次调用服务时,您将获得一个包含数组格式的10个元素的列表。因此,当您将此元素添加到数组时,请将一个额外的变量作为“isAdv”,并在完成循环后将其设置为“0”(您将向阵列添加元素)然后将此广告与此添加变量一起添加为“isAdv” “并设置”1“。所以现在你的数组将包含11个对象。在单元格配置检查

if(isAdv==1)
{
// this is your news cell
}
else
{
// this your normal cell to display your info which comes from server
}

详细示例

- (void)viewDidLoad
{
[super viewDidLoad];
menuTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 60, 320, 404) style:UITableViewStyleGrouped];
menuTableView.delegate=self;
menuTableView.dataSource=self;
menuTableView.separatorColor=[UIColor grayColor];
[self.view addSubview:menuTableView];
NSArray *serverResponseArray=[[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil];
finalArray =[[NSMutableArray alloc]init];

for (int i=0; i<serverResponseArray.count; i++) {

    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
    [dict setObject:serverResponseArray[i] forKey:@"cellinfo"];
    [dict setObject:@"false"  forKey:@"isAdv"];
    [finalArray addObject:dict];
}
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setObject:@"adv url " forKey:@"cellinfo"];
[dict setObject:@"true"  forKey:@"isAdv"];
[finalArray addObject:dict];
NSLog(@"%d",finalArray.count);
NSLog(@"%@",finalArray);

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return finalArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }

    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    if ([[finalArray[indexPath.row] objectForKey:@"isAdv"] isEqualToString:@"false"]) {

        cell.textLabel.text =[finalArray[indexPath.row] objectForKey:@"cellinfo"];

    }
    else
    {
        cell.textLabel.text =[finalArray[indexPath.row] objectForKey:@"cellinfo"]; // you can place imageview or something to show news here

    }
    NSLog(@"%f",tableView.contentOffset.y);
    NSLog(@"%d",[finalArray count]*40);



    return cell;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat height = scrollView.frame.size.height;

CGFloat contentYoffset = scrollView.contentOffset.y;

CGFloat distanceFromBottom = scrollView.contentSize.height - contentYoffset;

if(distanceFromBottom < height)
{
    NSLog(@"end of the table");
    [self callserviceagain];
    return;
}
}


-(void)callserviceagain
{
NSArray *serverResponseArray=[[NSArray alloc]initWithObjects:@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20", nil];

for (int i=0; i<serverResponseArray.count; i++) {

    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
    [dict setObject:serverResponseArray[i] forKey:@"cellinfo"];
    [dict setObject:@"false"  forKey:@"isAdv"];
    [finalArray addObject:dict];
}
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setObject:@"adv url " forKey:@"cellinfo"];
[dict setObject:@"true"  forKey:@"isAdv"];
[finalArray addObject:dict];
NSLog(@"%d",finalArray.count);
NSLog(@"%@",finalArray);
[menuTableView reloadData];
}
相关问题