我最近才开始为 iPhone 编程,正在制作一个连接到数据库并获取一组行名称并将它们显示出来的应用程序。当选中时,行的背景颜色会改变,即您可以进行多次选择,并且它们的颜色都不同。因此,我毫无问题地从服务器获取了 XML 并创建了一个 UITableView
(表格视图) 来显示单元格。但是,我不知道如何将单元格添加到表格中。我查看了 insertRowsAtIndexPaths
(插入指定位置的行) 但是我不确定该如何使用它?据我所知,insertRowsAtIndexPaths
接受两个参数:
一个包含单元格应该在哪一行及哪个部分的 NSArray(数组)。问题在于我的应用程序将具有动态数量的行。如果我不知道我将有多少行,我该如何创建 NSArray?我可以使用 NSMutableArray 吗?
它接受的第二个参数是一个动画 - 这很简单。
我遇到的另一个问题是:我应该在哪里创建这些单元格?如何将单元格传递给表视图?
我试过阅读文档,但文档似乎不是很清晰!这是我目前在视图控制器的 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];
cellForRowAtIndexPath
方法内的NSString *sEventType = [[eventTypes valueForKeyPath:@ "name.text"] objectAtIndex:indexPath.row];
出错。我需要添加滚动控制器或其他什么吗?再次感谢您迅速而有帮助的回应! - KerrM