IOS开发之TableView、多个TableViewCell、自定义Cell、Cell上画画(故事板+代码方式)

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

最近要做一个项目,有个账户设置界面,看了微博、微信、QQ,他们的账号设置都比较原生态没做什么处理。春雨医生的账号不错,做了许多处理。不说废话直接上代码。

第一步:

//UserTableViewCell.h这里定义第一种Cell#import<UIKit/UIKit.h>@interfaceUserTableViewCell : UITableViewCell

@PRoperty (weak, nonatomic) IBOutlet UIImageView*userviewcellicon;

@property (weak, nonatomic) IBOutlet UILabel*userviewcellname;@end

//UserTableViewCell2.h这里定义第二种Cell,#import<UIKit/UIKit.h>@interfaceUserTableViewCell2 : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel*name;

@property (weak, nonatomic) IBOutlet UIImageView*userviewcell2image;@end

故事板里的TableView和Cell的class和Cell的Identifier就不说了

例:

2、设计好了这些东西后,开始进TableViewController里了:

//UserTableViewController.h#import<UIKit/UIKit.h>@interfaceUserTableViewController : UITableViewController

@property(nonatomic,strong) NSDictionary*UserViewCellDic;

@property (nonatomic, strong) NSArray*listGroupname;@end

//UserTableViewController.m#import"UserTableViewController.h"#import"UserTableViewCell.h"#import"UserTableViewCell2.h"@interfaceUserTableViewController ()@end@implementationUserTableViewController- (void)viewDidLoad

{

[super viewDidLoad];self.tableView.separatorStyle = UITableViewCellaccessoryNone;//去除分割线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:)];//排序

}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView//返回有几个Section

{return[self.listGroupname count];

}- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section//Section头名字设为空

{NSString *groupName =@"";returngroupName;

}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section//每个section的行数

{

NSString*groupName =[self.listGroupname objectAtIndex:section];

NSArray*listitem =[self.UserViewCellDic objectForKey:groupName];return[listitem count];

}#pragmamark - TableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{staticNSString *UserViewCellIdentifier =@"UserTableViewCell";staticNSString *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 dequeueReusableCellWithIdentifier: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;//最后的箭头//画线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.0fgreen:43/255.0fblue:233/255.0falpha:1];

leftview.backgroundColor= [UIColor colorWithRed:43/255.0fgreen:43/255.0fblue:233/255.0falpha:1];

rightview.backgroundColor= [UIColor colorWithRed:43/255.0fgreen:43/255.0fblue:233/255.0falpha:1];

[cell.contentView addSubview:view];

[cell.contentView addSubview:leftview];

[cell.contentView addSubview:rightview];returncell;

}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;//最后的箭头//画线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.0fgreen:43/255.0fblue:233/255.0falpha:1];//alpha是透明度leftview.backgroundColor = [UIColor colorWithRed:43/255.0fgreen:43/255.0fblue:233/255.0falpha:0.3];

rightview.backgroundColor= [UIColor colorWithRed:43/255.0fgreen:43/255.0fblue:233/255.0falpha:1];

[cell.contentView addSubview:view];

[cell.contentView addSubview:leftview];

[cell.contentView addSubview:rightview];returncell;

}}-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath//Cell的高度

{

NSUInteger section=[indexPath section];if(0==section) {return80;

}else{return40;

}

}

这里附上Plist文件:

3、运行效果:

写的不是特别的详细,如果有什么不明白的或者需要源码留邮箱!

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航