【问题标题】:How to add one row with string to last row of UITableView in iOS?如何在iOS中的UITableView的最后一行添加一行字符串?
【发布时间】:2014-02-19 11:11:11
【问题描述】:

我使用了来自我网站的JSON 数据来获取Posts

当我得到JSON 20 个帖子的数据时,我会在UITableView 中显示该数据。

在我的情况下,我想在 tableView 的最后一行添加更多一行,字符串 "Load More Posts." 因为当我点击“加载更多帖子”行时,我需要重新加载我的 tableView 以显示另一行新帖子。

我正在使用NSMutableArrayNSDictionary 获取JSON 数据并在UITableView 中显示帖子。

那么我该如何添加呢?

【问题讨论】:

  • 如果您使用的是 iOS 6 或更高版本,则可以使用 UIRefreshControl。这是更标准的方式。
  • 在表格视图中添加页脚

标签: ios json uitableview nsmutablearray


【解决方案1】:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView  = [[[UIView alloc] init] autorelease];
    footerView.backgroundColor = [UIColor clearColor];
    footerView.frame=CGRectMake(0, 0, 253,100);

    UIButton *button = [[UIButton alloc] init];
    [button setFrame:CGRectMake(0, 0, 252, 50)];
    [button setTitle:@"Load More" forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
    [button setBackgroundColor:[UIColor clearColor]];
    [button.titleLabel setTextColor:[UIColor blackColor]];
    [button addTarget:self action:@selector(btn_LoadMore:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:button];
    tbl_location.tableFooterView.contentMode = UIViewContentModeScaleToFill;
    [button release];


    return footerView;
}

-(IBAction)btn_LoadMore:(id)sender
{
    tbl_location.hidden = TRUE;

    [self load_json_data];
}
-(void)load_json_data
{
    //some code

    [tbl_location reloadData];
    tbl_location.hidden = FALSE;
}

【讨论】:

  • 您只是在表格视图中添加按钮。您将如何更新表格
  • @mokujin use //Load JsonData //Reload Table in button click event
  • 当我用你的代码进行测试时,我的 tableView 一直隐藏,直到完成数据加载。完成加载数据后出现 UTIableView 兄弟。我该怎么做?
【解决方案2】:

您可以根据数组中的记录数手动处理条件。这是我给你一些示例代码,可以帮助你实现你想做的事情:

这里根据您在 Array 中的记录设法返回单元格中的行数。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if ( [jsonArray count] < 20 ) //20 Will change based on your number of records as loop progresses.
        {
                return [self.localJsonArray count];
        } else {
                return [self.localJsonArray count] + 1;
        }       
}

从数组中填充单元格中的数据并检查条件

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
        ImageCell *cell = (ImageCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (indexPath.row != [localJsonArray count] ) { // As long as we haven’t reached the +1 yet in the count, we populate the cell like normal
                if (cell == nil) {
                        cell = [[[ImageCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
                } 
                NSDictionary *itemAtIndex = (NSDictionary *)[self.localJsonArray objectAtIndex:indexPath.row];
                [cell setData:itemAtIndex];
        } // Ok, all done for filling the normal cells, next we probaply reach the +1 index, which doesn’t contain anything yet
        if ( [jsonArray count] == 20 ) { // Only call this if the array count is 25
                if(indexPath.row == [localJsonArray count] ) { // Here we check if we reached the end of the index, so the +1 row 
                        if (cell == nil) {
                                cell = [[[ImageCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
                        }
                        // Reset previous content of the cell, I have these defined in a UITableCell subclass, change them where needed
                        cell.cellBackground.image = nil;
                        cell.titleLabel.text = nil;
                        // Here we create the ‘Load More Posts.’ cell
                        loadMore =[[UILabel alloc]initWithFrame: CGRectMake(0,0,362,73)];
                        loadMore.textColor = [UIColor blackColor];
                        loadMore.highlightedTextColor = [UIColor darkGrayColor];
                        loadMore.backgroundColor = [UIColor clearColor];
                        loadMore.font=[UIFont fontWithName:@"Verdana" size:20];
                        loadMore.textAlignment=UITextAlignmentCenter;
                        loadMore.font=[UIFont boldSystemFontOfSize:20];
                        loadMore.text=@"Load More Posts...";
                        [cell addSubview:loadMore];
                }
        }
        return cell;
}

最后,保持事件处理并区分其他 ROW:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        if ( [jsonArray count] == 20 ) { //  Only call the function if we have 20 results in the array OR maintain its counts based on your array counts.
                if (indexPath.row == [localJsonArray count] ) {
                        NSLog(@"Load More requested"); // Add a function here to add more data to your array and reload the content
                } else {
                       NSLog(@"Normal cell selected"); // Add here your normal didSelectRowAtIndexPath code
               }
        } else {
                       NSLog(@"Normal cell selected with < 20 results"); //  Add here your normal didSelectRowAtIndexPath code
        }
}

【讨论】:

  • @为什么否定这个?有人可以告诉我,这个解决方案有什么问题吗? Yahiko 你有没有在你的代码中尝试过这个......
  • 对不起,我要出去了。我只是投赞成票,投反对票不是我。我来检查一下。谢谢:)
猜你喜欢
  • 2011-01-17
  • 2023-04-01
  • 2015-02-26
  • 1970-01-01
  • 2020-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多