【发布时间】:2012-02-24 11:02:13
【问题描述】:
我最近刚刚开始为 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];
【问题讨论】:
标签: ios objective-c iphone uitableview