簡體   English   中英

將新行添加到uitableview的末尾

[英]add new row to end of 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;
}

您可以對UITableView使用insertAtIndexPath方法在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獲取數據。

我認為這會對您有所幫助。

代碼不作完整意義上的對我來說,它看起來像你有兩個不同的類, SubViewControllerSubCategoryViewController ,其中一個包含的tableView? 您正在使用標識符“ SubView”實例化這兩個實例。 在我看來,您可以刪除整個SubCategoryViewController-class,而僅使用SubViewController。 我可能會誤解他們的目的。

無論哪種方式,您都需要在cellForRowAtIndexPath方法中實例化一個新的空View Controller。 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.. ,請評論兩者之間的區別,我會揣摩出來。

嘗試此希望對您有幫助。 首次致電服務時,您將獲得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];
}

暫無
暫無

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

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