IOS development-custom cell implementation

Source: Internet
Author: User

Custom Cells

When the cell styles provided by Apple cannot meet our business needs, we need to customize cells. Before iOS 5, custom cells can be implemented in two ways: code implementation and XIB technology implementation. It is relatively simple to use the XIB technology. Create an XIB file and define a cell class that inherits the uitableviewcell class. After iOS 5, we had a new option, the implementation method of the story board, which is simpler than the XIB method.

 

Modify the prototype diagram of the simple table view case. In this case, the four built-in cell styles are not suitable.

Use the "single view application" Project template to create a project named "customcell". Set the "prototype cells" project of the table view attribute to 1 (the other operations are the same as the previous one ).

 

There is a cell design screen in the upper part of the design screen. We can design the Cell Layout at this position. Drag a label and Image view from the object library to the cell design screen and adjust their positions.

 

Create a custom cell class customcell and select uitableviewcell as the parent class.

 

Return to the IB design screen and select "Table View Controller scene"> "Table View Controller"> "Table view cell" on the left of IB ", open the cell id checker and select customcell class in the class option.

 

Connect the output port of the lable and imageview controls

 

The code for this case is as follows:

////  CustomCell.h//  CustomCell#import <UIKit/UIKit.h>@interface CustomCell : UITableViewCell@property (weak, nonatomic) IBOutlet UILabel *name;@property (weak, nonatomic) IBOutlet UIImageView *image;@end////  CustomCell.m//  CustomCell#import “CustomCell.h”@implementation CustomCell@end

 

 

The customcell class code is relatively simple, and actions need to be defined in some businesses.

Modify tableview: cellforrowatindexpath in viewcontroller. m. The Code is as follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{static NSString *CellIdentifier = @”Cell”;CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];    }NSUInteger row = [indexPath row];NSDictionary *rowDict = [self.listTeams objectAtIndex:row];cell.name.text =  [rowDict objectForKey:@"name"];cell.image.image = [UIImage imageNamed:[rowDict objectForKey:@"image"]];NSUInteger row = [indexPath row];NSDictionary *rowDict = [self.listFilterTeams objectAtIndex:row];cell.textLabel.text =  [rowDict objectForKey:@"name"];NSString *imagePath = [rowDict objectForKey:@"image"];imagePath = [imagePath stringByAppendingString:@".png"];cell.image.image = [UIImage imageNamed:imagePath];cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;return cell;}

 

We can see that the IF (cell = nil) {} code is removed, because we have set the reuse ID to cell in IB. The other code in the method is the same as that in the simple table. Run it.

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.