使用自定义单元格在 TableView 中的任何索引处插入一行 [英] Insert one row at any index in TableView with custom Cell

查看:31
本文介绍了使用自定义单元格在 TableView 中的任何索引处插入一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 iOS 新手.我尝试了更多时间进行搜索,但结果并非我所愿.首先,我有 5 行带有自定义单元格.单元格仅包含文本,我想在包含一张图像的单元格的任何索引处添加一行.那么我们如何实现它?

I'm newbie on iOS. I tried more time to search but the result is not my wish. The first, I have 5 rows with the custom cell. Cell contains only text and I want to add one row at any index with cell that contains one image. So how we can implement it ?

推荐答案

从评论中我了解到您对 UITableView 非常陌生.所以请阅读一些给定的教程.您将能够在 UITableView 中添加、删除编辑单元格

From the comments i came to know you are very new to UITableView. So please go through some of the given tutorial. You will be able to add, remove edit cells in UITableView

UITableView 教程
教程 2
表视图编程指南

对于简单的插入,这样做

For simple insertion do like this

将按钮及其动作添加到视图中

Add button and its action to the view

 -(IBAction)addNewRow:(UIButton *)sender
 {
  self.numberOfRows++;  // update the data source    
  NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
   [self.tableview insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

 }

编辑

我认为你需要这样的东西

I think you need some thing like this

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellID1=@"CellOne";
NSString *cellID2=@"CellTwo";

UITableViewCell *cell = nil;

if (0 == indexPath.row) {

    cell =[tableView dequeueReusableCellWithIdentifier:cellID1];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID1];
    }
    cell.textLabel.text = @"New Cell";

}

else
{
    cell =[tableView dequeueReusableCellWithIdentifier:cellID2];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID2];
    }
    cell.imageView.image = [UIImage imageNamed:@"Test.jpg"];
}

return cell;
}

不要忘记增加单元格计数,即现在从 numberOfRows 返回 6.

Dont forget increment the cell count ie now return 6 from numberOfRows.

这篇关于使用自定义单元格在 TableView 中的任何索引处插入一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆