Loading Custom Table-View Cells From Nib Files

左心房为你撑大大i 提交于 2019-12-01 21:13:53

The owner is your implementation of the table view controller. In your table view controller you define a UITableViewCell property (in this case it is tvCell)

@interface MyTableViewController: UITableViewController {

    IBOutlet UITableViewCell *tvCell;

    @property (nonatomic, retain) IBOutlet UITableViewCell *tvCell;

Then in your nib file for the custom table view cell you specify the files owner as of type MyTableViewController and point the tvCell outlet to the table cell view in the nib.

Then in the cellForRowAtIndex path the following line:

[[NSBundle mainBundle] loadNibNamed:@"TVCell" owner:self options:nil];

this line loads the cells nib, setting your table view controller (i.e. self) as the owner, thus connecting the tvCell property in your table view controller to point to the TableViewCell in the nib.

You can then take a copy of that pointer and initialise the fields in the cell in this method and return that 'custom' cell from the method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!