简体   繁体   中英

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 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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM