Objective-c:uitableviewcell a custom cell

Source: Internet
Author: User



UITableViewCell: Custom cells, you can create cells in xib, or you can create cells in Storyborad. There are four ways to create


<1> cells created in Storyborad, which are static cells, are available at the beginning of the cell and can be loaded directly according to the custom reuse identity name; <2> Of course, cells in Storyborad can also be associated with a custom class, which must be inherited UITableViewCell, which can be loaded directly based on the custom reuse identity name. <3> cells created in Xib can be loaded directly from the reuse identifier if they are loaded directly through the Bundel Loadnibnme method;<4> Of course, the cells in the Xib file can be associated with a custom class that must be inherited UITableViewCell, in which case it is not feasible to load using the custom reuse identifier directly, Because the proxy method does not initialize the Cell object at this time, it is necessary to encapsulate the process of creating the Cell object into its own associated class, that is, a class method of the created cell is used to load the Xib file, an instance method of a class object that is used to set the properties in the cell. method One: Create and load directly in the storyboard   you need to set the reuse identifier for the cell:The code is as follows:a class created for initializing data:
 
 
1 #import <Foundation/Foundation.h>
2 
3 @interface Contact : NSObject
4 @property (copy,nonatomic)NSString *name;
5 @property (copy,nonatomic)NSString *faceName;
6 -(instancetype)initWithName:(NSString*)name andFaceName:(NSString*) faceName;
7 @end
 
 1 #import "Contact.h"
 2 
 3 @implementation Contact
 4 -(instancetype)initWithName:(NSString*)name andFaceName:(NSString*) faceName
 5 {
 6     self = [super init];
 7     if(self)
 8     {
 9         _name = [name copy];
10         _faceName = [faceName copy];
11     }
12     return self;
13 }
14 @end


  In the attempt to complete the code in the controller:


1 #import "ViewController.h"
 2 #import "Contact.h"
 3 @interface ViewController ()<UITableViewDataSource>
 4 @property (weak, nonatomic) IBOutlet UITableView *tableView;
 5 @property (strong,nonatomic)NSMutableArray *contacts;
 6 @end
 7
 8 @implementation ViewController
 9 
10-(void)viewDidLoad {
11 [super viewDidLoad];
12 //Initialize data
13 self.contacts = [NSMutableArray arrayWithCapacity:9];
14 for(int i=0; i<9; i++)
15 {
16 Contact *conatct = [[Contact alloc]initWithName:[NSString stringWithFormat:@"name%d",i+1] andFaceName:[NSString stringWithFormat:@"%d.png",i]];
17 [self.contacts addObject:conatct];
18}
19
20 //Set the data source of tableView
21 self.tableView.dataSource = self;
twenty two }
twenty three 
24 #pragma mark -data source method of tableView
25 //How many rows in each group
26 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
27 {
28 return self.contacts.count;
29}
30 //Set the content of each cell
31 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
32 {
33 //1. According to reuseIdentifier, first go to the object pool to find the reused cell object
34 static NSString *reuseIdentifier = @"myCell";
35 UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
36 //2. Set the content of the cell object
37 Contact *contact = [self.contacts objectAtIndex:indexPath.row];
38 UILabel *label = (UILabel*)[cell viewWithTag:1];
39 label.text = contact.name;
40 UIImageView *imageView = (UIImageView*)[cell viewWithTag:2];
41 [imageView setImage:[UIImage imageNamed:contact.faceName]];
42 return cell;
43}
44
45 @end 





Objective-c:uitableviewcell a custom cell


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.