TableView for IOS development, multiple tableviewcells, custom Cell, and Cell painting (storyboards + code), ios7tableview Index

Source: Internet
Author: User

TableView for IOS development, multiple tableviewcells, custom Cell, and Cell painting (storyboards + code), ios7tableview Index

Recently, I am going to create a project with an account setting interface. After reading Weibo, QQ, and their account settings, I did nothing to deal with them. Dr. Chun has a good account and has done a lot of work. Directly add the code without talking nonsense.

Step 1:

// UserTableViewCell. h define the first Cell # import <UIKit/UIKit. h> @ interface UserTableViewCell: UITableViewCell @ property (weak, nonatomic) IBOutlet UIImageView * userviewcellicon; @ property (weak, nonatomic) IBOutlet UILabel * userviewcellname; @ end
// UserTableViewCell2.h the second Cell is defined here, # import <UIKit/UIKit. h> @ interface UserTableViewCell2: UITableViewCell @ property (weak, nonatomic) IBOutlet UILabel * name; @ property (weak, nonatomic) IBOutlet UIImageView * userviewcell2image; @ end

The TableView in the story board and the Cell class and Cell Identifier will not be mentioned.

Example:

 

 

2. After designing these things, we started to enter TableViewController:

//UserTableViewController.h#import <UIKit/UIKit.h>@interface UserTableViewController : UITableViewController@property(nonatomic,strong) NSDictionary *UserViewCellDic;@property (nonatomic, strong) NSArray *listGroupname;@end
// UserTableViewController. m # import "UserTableViewController. h "# import" UserTableViewCell. h "# import" UserTableViewCell2.h "@ interface UserTableViewController () @ end @ implementation UserTableViewController-(void) viewDidLoad {[super viewDidLoad]; self. tableView. separatorStyle = break; // remove the split line NSBundle * bundle = [NSBundle mainBundle]; NSString * plistpath = [bundle pathForResource: @ "UserViewCell" ofType: @ "plist"]; self. userViewCellDic = [[NSDictionary alloc] initWithContentsOfFile: plistpath]; self. listGroupname = [self. userViewCellDic allKeys]; self. listGroupname = [self. listGroupname sortedArrayUsingSelector: @ selector (compare :)]; // sorting}-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView // Several sections are returned {return [self. listGroupname count];}-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section // The Section header name is set to null {NSString * groupName = @""; return groupName;}-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section // The number of lines in each section {NSString * groupName = [self. listGroupname objectAtIndex: section]; NSArray * listitem = [self. userViewCellDic objectForKey: groupName]; return [listitem count];} # pragma mark-TableViewDataSource-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * UserViewCellIdentifier = @ "UserTableViewCell"; static NSString * UserViewCellIdentifier2 = @ "UserTableViewCell2"; NSUInteger section = [indexPath section]; NSUInteger row = [indexPath row]; NSString * groupName = [self. listGroupname objectAtIndex: section]; NSArray * listitem = [self. userViewCellDic objectForKey: groupName]; NSDictionary * rowDict = [listitem objectAtIndex: row]; if (0 = section) {UserTableViewCell2 * cell = [tableView partition: UserViewCellIdentifier2]; cell. name. text = [rowDict objectForKey: @ "name"]; NSString * imagePath = [rowDict objectForKey: @ "image"]; imagePath = [imagePath stringByAppendingString :@". png "]; cell. userviewcell2image. image = [UIImage imageNamed: imagePath]; cell. accessoryType = UITableViewCellAccessoryDisclosureIndicator; // The last arrow // draw line UIView * view = [[UIView alloc] initWithFrame: CGRectMake (0, 79,320, 1)]; UIView * leftview = [[UIView alloc] initWithFrame: CGRectMake (0, 0, 3, 80)]; UIView * rightview = [[UIView alloc] initWithFrame: CGRectMake (317, 0, 3, 80)]; view. backgroundColor = [UIColor colorwithred: 43/255. 0f green: 43/255. 0f blue: 233/255. 0f alpha: 1]; leftview. backgroundColor = [UIColor colorwithred: 43/255. 0f green: 43/255. 0f blue: 233/255. 0f alpha: 1]; rightview. backgroundColor = [UIColor colorwithred: 43/255. 0f green: 43/255. 0f blue: 233/255. 0f alpha: 1]; [cell. contentView addSubview: view]; [cell. contentView addSubview: leftview]; [cell. contentView addSubview: rightview]; return cell;} else {UserTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: UserViewCellIdentifier]; cell. userviewcellname. text = [rowDict objectForKey: @ "name"]; NSString * imagePath = [rowDict objectForKey: @ "image"]; imagePath = [imagePath stringByAppendingString :@". png "]; cell. userviewcellicon. image = [UIImage imageNamed: imagePath]; cell. accessoryType = UITableViewCellAccessoryDisclosureIndicator; // The last arrow // draw line UIView * view = [[UIView alloc] initWithFrame: CGRectMake (0, 39,320, 1)]; UIView * leftview = [[UIView alloc] initWithFrame: CGRectMake (0, 0, 3, 40)]; UIView * rightview = [[UIView alloc] initWithFrame: CGRectMake (317, 0, 3, 40)]; view. backgroundColor = [UIColor colorwithred: 43/255. 0f green: 43/255. 0f blue: 233/255. 0f alpha: 1]; // alpha is the transparency leftview. backgroundColor = [UIColor colorwithred: 43/255. 0f green: 43/255. 0f blue: 233/255. 0f alpha: 0.3]; rightview. backgroundColor = [UIColor colorwithred: 43/255. 0f green: 43/255. 0f blue: 233/255. 0f alpha: 1]; [cell. contentView addSubview: view]; [cell. contentView addSubview: leftview]; [cell. contentView addSubview: rightview]; return cell;}-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath // Cell height {NSUInteger section = [indexPath section]; if (0 = section) {return 80 ;}else {return 40 ;}}

The Plist file is attached here:

 

 

3. Running effect:

 

It is not particularly detailed. If you do not understand anything or you need to leave your email address with the source code!


How can I add a custom Cell to a custom TableView in iOS? Can I select a row to display the buttons on the Cell?

There are two methods for customizing cells. One is to use the nib file and the other is to directly write controls in the code.
Let's take a look at this.
Blog.csdn.net/..790487
Cell .. Write events in the method. When you click it, the button is displayed.
 
For ios development, ask how to do this tableVIew and write it in full code.

You need to customize three uitableviewcells.
The first is the cell with buttons in the first line.
@ Interface buttonCell: UITableViewCell @ property (nonatomic, retain) IBOutlet UIButton * btn; @ end the second line is the cell with textfield on the right of the second line
@ Interface textfieldCell: UITableViewCell @ property (nonatomic, retain) IBOutlet UITextField * textfield; @ end the third is the cell from the third row to the end.
@ Interface selectCell: UITableViewCell @ property (nonatomic, retain) IBOutlet UILabel * lblTitle; @ property (nonatomic, retain) IBOutlet UILabel * lblselectedText; @ end

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.