开发者

Add NSMutableArray in TableView Which contains set of Views

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 any开发者_如何学Pythonthing 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


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


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: actually expects the first parameter to be an array of index paths, not the views.

So the correct usage would be like:

[tblView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:4]] withRowAnimations:UITableViewRowAnimationAutomatic];

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜