在包含视图集的 TableView 中添加 NSMutableArray
这就是我想要做的,我用一张图像和 3 个标签设置了视图,每个都是相同的。
[viewarray addObject:xView]; //view array is NSMutable Array and I am adding 5-6 views
[tblView insertRowsAtIndexPaths:viewarray withRowAnimation:UITableViewRowAnimationFade];
此代码不会给出任何错误,但也不会在表中添加任何内容。
我做错了什么,如果可能的话,请给我代码片段,以使用 One UIImageView
+ 3 个标签左侧图像和右侧 3 个标签创建 Custom UIView
Here is what I am trying to do, I have set View with one Image and 3 Label, each are identical.
[viewarray addObject:xView]; //view array is NSMutable Array and I am adding 5-6 views
[tblView insertRowsAtIndexPaths:viewarray withRowAnimation:UITableViewRowAnimationFade];
This code doesn't give any error, but also it doesn't add anything in table.
What I am doing wrong, Also if Possible please give me code snippet to create Custom UIView
with One UIImageView
+ 3 Lables left side Image and right side 3 Labels
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
UITableView 不使用 UIView 作为单元格。它实际上使用了 UITableViewCell。
insertRowsAtIndexPaths:withRowAnimations:
方法需要一个 NSIndexPath 的 NSArray。UITableView中的每一个UITableViewCell对应一个NSIndexPath。 NSIndexPath 保存 UITableView 中特定 UITableViewCell 的顺序和节号。
例如:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
这将为第 1 部分的第 0 行(第一个单元格)创建一个 NSIndexPath。
创建 NSIndexPath 后,您可以使用
insertRowsAtIndexPaths:withRowAnimations:
。[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
这将使 UITableView 调用
cellForRowAtIndexPath:
以使用您输入的信息创建一个新单元格NSIndexPath。您可以在此处获取有关 UITableViewCell 的更多信息:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/cl/UITableViewCell
或者您可以在此处查看表视图编程指南:
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html#//apple_ref/doc/uid /TP40007451-CH1-SW1
UITableView don't use UIViews as cells. It actually uses the UITableViewCell.
The method
insertRowsAtIndexPaths:withRowAnimations:
expects a NSArray of NSIndexPath.Each UITableViewCell in the UITableView corresponds to one NSIndexPath. The NSIndexPath holds the order and section number of a particular UITableViewCell in the UITableView.
For example:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
This will create a NSIndexPath for the row 0 (first cell) on the section 1.
After you create the NSIndexPath you can use the
insertRowsAtIndexPaths:withRowAnimations:
.[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
This will make the UITableView to call
cellForRowAtIndexPath:
to create a new cell with the information that you put in the NSIndexPath.You can get more information about UITableViewCell in here:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/cl/UITableViewCell
Or you can check out the Table View Programming Guide here:
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html#//apple_ref/doc/uid/TP40007451-CH1-SW1
您需要查看 UITableView 和 UITableViewCell 信息。
表视图使用 UITableViewCell 对象来显示单元格。
insertRowsAtIndexPaths 方法只是通知 UITableView 需要添加一些单元格。
它是 tableView:cellForRowAtIndexPath: 方法(UITableViewDataSource 协议),用于设置表格的单元格。
You need to look at the UITableView and UITableViewCell informations.
A table view uses UITableViewCell objects to show cells.
The insertRowsAtIndexPaths method simply informs the UITableView that some cells needs to be added.
It's the tableView:cellForRowAtIndexPath: method (of UITableViewDataSource protocol) which is used to set cells of the table.
insertRowsAtIndexPaths:withRowAnimations:
实际上期望第一个参数是索引路径数组,而不是视图。因此,正确的用法如下:
在这种情况下,表视图将自动要求其数据源通过调用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath * 为这些索引路径提供实际的单元格视图)indexPath
insertRowsAtIndexPaths:withRowAnimations:
actually expects the first parameter to be an array of index paths, not the views.So the correct usage would be like:
In this case table view will automatically ask its data source to provide actual cell views for those index paths through calling
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath