以编程方式将单元格添加到UITableView [英] Adding cells programmatically to UITableView

查看:85
本文介绍了以编程方式将单元格添加到UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've just recently started programming for the iPhone and I'm making an application that connects to a database and gets a set of row names and displays them. When selected, the rows background colour change ie you can make multiple selections and they will all be different colours. So I'm getting the XML back from the server no problem and I have created a UITableView to display the cells in. However, I have no idea how to add the cells into the table. I had a look at insertRowsAtIndexPaths but I'm not sure how to use it? As I understand, insertRowsAtIndexPaths takes two parameters:

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

An NSArray that contains what row the cell is supposed to be in and in what section. The problem with this is that my app will have a dynamic number of rows. How would I go about creating the NSArray if I don't know how many rows I will have? Can I use NSMutableArray?

它所采用的第二个参数是一个动画 - 这非常简单。

The second parameter it takes is an animation - that's pretty straightforward.

另一个问题我在哪里实际创建细胞?你如何将细胞传递到tableview?

Another issue I am having is where do I actually create the cells? How do you pass the cells to the tableview?

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

I've tried reading the documentation but it just doesn't seem very clear! Here is the code I have at the moment inside the loadview method of the view controller:

 //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];


推荐答案

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

I think you're approaching this from the wrong direction. UITableViews don't work as you are expecting. insertRowsAtIndexPaths is for inserting new rows into a table, rather than for populating it in the first instance.

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

UITableViews work by calling a number of delegate methods that allow you to present your data to the table view as you need to. The framework then takes care of the heavy lifting to populate cells, handle scrolling and touch events, etc.

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

I would recommend that you start by reading a tutorial such as this one: http://www.iosdevnotes.com/2011/10/uitableview-tutorial/ , which looks fairly thorough to me. It explains how to set your datasource for the table and how you can configure the way your data is presented by the UITableView.

祝你好运!

这篇关于以编程方式将单元格添加到UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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