[ios]tablbviewcell

1. Create a tableview on the storyboard and pull it into the controller to create a link

 

2. Storyboard establishes constraints: pull the dataSource delegate to the small button of the controller view on the connector

Inherit UIViewController<UITableViewDataSource, UITableViewDelegarte> on controller.h

 

3. Add the method in controller.m:

numberOfRowsInSection (note whether counts really has a value here)

cellForRowAtIndewPat

 

4. Add tableViewCell:

(1) Pull up the tableViewCell on the tableview of the storyboard and set the style

 (2) Create a tableviewcell class and link the style controls to .h

(3) The Custom Class on the storyboard changes to the class, and the identifier is named on the Identifier

 

5. Point to the cell in the code:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIden = @"deviceListCell";
    
    DeviceListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIden forIndexPath:indexPath];
    
    cell.deviceName.text = @"test";//for test

    return cell;
}

 

6. Adjust the height of the cell

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

 

2. Pure code custom cell

Reference: http://blog.csdn.net/u012350430/article/details/51181728

重写-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier:

 

//cell customization uses -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier method
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        //By the way, the creation of small UIButton is introduced here
        //Set the type of button to UIButtonTypeRoundedRect
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        //Set the frame of the button
        button.frame = CGRectMake(20, 20, 50, 50);

        //The button's normal state title is set to Yes, and the selected state's title is set to No
        [button setTitle:@"Yes" forState:UIControlStateNormal];
        [button setTitle:@"No" forState:UIControlStateSelected];

        //The way to set the button to respond to the click event is buttonPressed:
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        //Add to cell
        [self addSubview:button];

        //Create an imageView and add it to the cell
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Totoro副本"]];
        imageView.frame = CGRectMake(150, 20, 150, 100);
        [self addSubview:imageView];

    }
    return self;
}

//buttonPressed: method
-(void)buttonPressed:(UIButton *)button
{
    // Realize the switch state of the button
    button.selected = !button.selected;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326394281&siteId=291194637
ios