以编程方式将单元格添加到UITableView

Ker*_*rrM 12 iphone objective-c uitableview ios

我刚刚开始为iPhone编程,我正在创建一个连接到数据库并获取一组行名称并显示它们的应用程序.选择后,行背景颜色会发生变化,即您可以进行多项选择,它们都将是不同的颜色.所以我从服务器返回XML没问题,我创建了一个UITableView显示单元格.但是,我不知道如何将单元格添加到表格中.我看了一下,insertRowsAtIndexPaths但我不确定如何使用它?据我了解,insertRowsAtIndexPaths需要两个参数:

一个NSArray,包含单元格应该在哪个行以及在哪个部分.这个问题是我的应用程序将有一个动态的行数.如果我不知道我将拥有多少行,我将如何创建NSArray?我可以使用NSMutableArray吗?

它需要的第二个参数是动画 - 这非常简单.

我遇到的另一个问题是我在哪里创建单元格?你如何将细胞传递到tableview?

我试过阅读文档,但它似乎不太清楚!这是我在视图控制器的loadview方法中的代码:

 //Before this I get the XML from the server so I am ready to populate
 //cells and add them to the table view
 NSArray *cells = [NSArray arrayWithObjects:
                   [NSIndexPath indexPathForRow:0 inSection:0],
                   [NSIndexPath indexPathForRow:1 inSection:0],
                   [NSIndexPath indexPathForRow:2 inSection:0],
                   [NSIndexPath indexPathForRow:3 inSection:0],
                   [NSIndexPath indexPathForRow:4 inSection:0],
                   [NSIndexPath indexPathForRow:5 inSection:0],
                   [NSIndexPath indexPathForRow:6 inSection:0],
                   [NSIndexPath indexPathForRow:7 inSection:0],
                   [NSIndexPath indexPathForRow:8 inSection:0],
                   [NSIndexPath indexPathForRow:9 inSection:0],
                   [NSIndexPath indexPathForRow:10 inSection:0],
                   [NSIndexPath indexPathForRow:11 inSection:0],
                   [NSIndexPath indexPathForRow:12 inSection:0],
                   nil];
[eventTypesTable beginUpdates];
[eventTypesTable insertRowsAtIndexPaths:cells withRowAnimation:UITableViewRowAnimationNone];
[eventTypesTable endUpdates];
Run Code Online (Sandbox Code Playgroud)

Wil*_*ell 18

我想你是从错误的方向接近这个.UITableViews无法正常工作.insertRowsAtIndexPaths用于将新行插入表中,而不是在第一个实例中填充它.

UITableViews通过调用许多委托方法来工作,这些方法允许您根据需要将数据呈现给表视图.然后,框架负责填充单元格,处理滚动和触摸事件等.

我建议您首先阅读这样的教程:http://www.iosdevnotes.com/2011/10/uitableview-tutorial/,这看起来相当彻底.它解释了如何为表设置数据源以及如何配置UITableView显示数据的方式.

祝好运!

  • 链接坏了.这就是为什么你至少应该发布一些示例代码而不是依赖外部链接. (7认同)

Fra*_*ade 16

不需要使用insertRowsAtIndexPaths.

检查:UITableViewDataSource协议参考UITableView类参考

魔法发生在这三种方法之间(UITableViewDataSource协议方法):

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
Run Code Online (Sandbox Code Playgroud)

你只需要填充一个数组.是的,它可以是一个NSMutableArray.

您可以填充数组- (void)viewDidLoad,例如:

yourItemsArray = [[NSMutableArray alloc] initWithObjects:@"item 01", @"item 02", @"item 03", nil];
Run Code Online (Sandbox Code Playgroud)

他们使用这样的数据源方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    // If You have only one(1) section, return 1, otherwise you must handle sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [yourItemsArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    cell.textLabel.text = [yourItemsArray objectAtIndex:indexPath.row];

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

像这样,将自动创建单元格.

如果你挑战数组,只需要调用:

[self.tableView reloadData];
Run Code Online (Sandbox Code Playgroud)

  • 没关系,对同一个问题有不同的方法.如果您开始学习,您应该学习教程以了解工作原理,而不仅仅是代码.但两者都有帮助 (3认同)