【问题标题】:making a "load more" button in uitableviewcell?在uitableviewcell中制作“加载更多”按钮?
【发布时间】:2011-05-28 05:26:23
【问题描述】:

嘿, 谁能指导我完成这个我在表中有大约 15 个条目我希望另外 15 个条目在最后一个 UITableViewCell 中提供更多负载。谁能帮帮我?

【问题讨论】:

标签: objective-c cocoa-touch uitableview ios4


【解决方案1】:

在表格视图中显示一个额外的行,在

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return dataRows+1;
    }

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

        //after setting tableviewcell

        if(indexPath.row==dataRows){

        cell.textLabel.text=@"Load More Rows";
        }
    }

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {

    if(indexPath.row==dataRows){
    //there you can write code to get next rows
    }
 }

您需要根据显示的行更新 numberOfRows 变量。

编辑:获取额外条目后,您可以使用以下方法将它们添加到现有条目数组中。您的原始数组应该是 NSMutableArray 才能使用此方法。

[originalEntriesArray addObjectsFromArray:extraEntriesArray];

【讨论】:

  • @SriPriya,添加此行numberOfRows = [yourEntriesArray count] + 1; 使其更清晰。
  • 所以我要添加 +1 以返回 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 吗?
  • @SriPriya,我已编辑您的答案以添加更多上下文。我希望你不会介意。 :)
  • @user655995:是的,它说,如果 dataRows 为 15,我们将 numberOfRows 设置为 16 以显示额外的一行显示“加载更多行”
  • 知道如何使用自定义单元格吗?
【解决方案2】:

我写了一个例子项目就是这样做的。从 GitHub 下载 https://github.com/Abizern/PartialTable

【讨论】:

  • 我喜欢使用beginUpdateendUpdate更新tableView的部分
【解决方案3】:

我写了一些可能有用的东西:https://github.com/nmondollot/NMPaginator

它封装了分页,几乎可以与任何使用 page 和 per_page 参数的 Web 服务一起使用。它还具有 UITableView,当您向下滚动时会自动获取下一个结果。

【讨论】:

    【解决方案4】:

    希望对你有帮助

    我取了一个可变数组和一个整数变量,并将数组的总数设置为整​​数变量

    arr = [[NSMutableArray alloc]initWithObjects:@"Radix",@"Riki", nil]; dataRows = [arr 计数];

    然后我根据表格的数据源方法中的整数变量设置部分中的行数

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 返回节中的行数。 返回数据行+1; }

    因为你最后想要一个额外的单元格。

    现在是时候设置表格单元格的文本了

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

    { 静态 NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
    
    //setting the text of the cell as per Mutable array
    if (indexPath.row < [arr count]) {
    
        cell.textLabel.text = [arr objectAtIndex:indexPath.row];
    }
    
     //setting the text of the extra cell
    
    if (indexPath.row == dataRows) {
        cell.textLabel.text = @"more cells";
    }     
    return cell;
    

    }

    现在点击更多单元格,您需要额外的单元格,所以只需在

    中添加您的代码 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    方法,意味着你必须做这样的事情

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.row == dataRows) { //请为exra细胞编码 } }

    运行您的应用以检查此代码。

    【讨论】:

      猜你喜欢
      • 2019-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-08
      • 2013-03-13
      • 2016-06-12
      相关资源
      最近更新 更多