首先来看下我们要实现的效果
需要实现这样的效果
然后我们开始动手吧。
首先选择添加一个新的ViewController
然后打开XIB文件,添加一UITableView 并将样式设置为分组
同时将按住CONTROL 链接dataSource与delegate
接着修改.H文件,具体代码如下
1 #import <UIKit/UIKit.h> 2 3 @interface GRXXViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> 4 { 5 NSString *name; 6 NSString *uid; 7 NSString *sex; 8 NSString *address; 9 NSString *gxqm; 10 } 11 @property(nonatomic,retain) NSString *name; 12 @property(nonatomic,retain) NSString *uid; 13 @property(nonatomic,retain) NSString *sex; 14 @property(nonatomic,retain) NSString *address; 15 @property(nonatomic,retain) NSString *gxqm; 16 @end
然后我们还需要自定义一个CELL来显示相关的样式
具体样式如下
并修改.h文件和.m文件 ,同时将两个label 与代码进行绑定
1 #import <UIKit/UIKit.h> 2 3 @interface infoCell : UITableViewCell 4 { 5 UILabel *contentlabel; 6 UILabel *titilelabel; 7 } 8 @property(nonatomic,retain) IBOutlet UILabel *contentlabel; 9 @property(nonatomic,retain) IBOutlet UILabel *titilelabel; 10 @end
1 #import "infoCell.h" 2 3 @implementation infoCell 4 @synthesize contentlabel; 5 @synthesize titilelabel; 6 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 7 { 8 self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 9 if (self) { 10 // Initialization code 11 } 12 return self; 13 } 14 15 - (void)setSelected:(BOOL)selected animated:(BOOL)animated 16 { 17 [super setSelected:selected animated:animated]; 18 19 // Configure the view for the selected state 20 } 21 22 @end
然后 选择GRXXViewController.m 文件
完成
//定义分组数
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
//定义分组行数
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//设置分组行头
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
这几个方法
直接上代码
1 #import "GRXXViewController.h" 2 #import "infoCell.h" 3 @interface GRXXViewController () 4 5 @end 6 7 @implementation GRXXViewController 8 @synthesize name; 9 @synthesize gxqm; 10 @synthesize sex; 11 @synthesize uid; 12 @synthesize address; 13 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 14 { 15 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 16 if (self) { 17 // Custom initialization 18 } 19 return self; 20 } 21 22 - (void)viewDidLoad 23 { 24 name=@"请输入中文名"; 25 uid=@"myzhanghao"; 26 sex=@"男"; 27 address=@"浙江温州"; 28 gxqm=@"IOS开发中"; 29 [super viewDidLoad]; 30 // Do any additional setup after loading the view from its nib. 31 } 32 33 - (void)didReceiveMemoryWarning 34 { 35 [super didReceiveMemoryWarning]; 36 // Dispose of any resources that can be recreated. 37 } 38 #pragma mark - 39 #pragma mark Table View Data Source Methods 40 //定义分组数 41 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{ 42 return 3; 43 } 44 //定义分组行数 45 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 46 if(section==0) 47 return 2; 48 else if(section==1) 49 return 3; 50 else 51 return 1; 52 } 53 //设置分组行头 54 -(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 55 // if(section==0) 56 // return @"基本信息"; 57 // else if(section==1) 58 // return @"总计"; 59 // else if(section==2) 60 // return @"与互"; 61 // else if(section==3) 62 // return @"查询"; 63 // else 64 return @""; 65 } 66 -(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 67 { 68 if([indexPath section]==0) 69 { 70 if([indexPath row]==0) 71 { 72 static NSString *modifyinfoTableIdentifier=@"LookInfoModelCell"; 73 infoCell *cell= (infoCell *)[tableView dequeueReusableCellWithIdentifier:modifyinfoTableIdentifier]; 74 if(cell==nil){ 75 NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"infoCell" owner:self options:nil]; 76 cell = [nib objectAtIndex:0]; 77 } 78 cell.contentlabel.text=name; 79 cell.titilelabel.text=@"名字:"; 80 return cell; 81 } 82 else 83 { 84 static NSString *modifyinfoTableIdentifier=@"LookInfoModelCell"; 85 infoCell *cell= (infoCell *)[tableView dequeueReusableCellWithIdentifier:modifyinfoTableIdentifier]; 86 if(cell==nil){ 87 NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"infoCell" owner:self options:nil]; 88 cell = [nib objectAtIndex:0]; 89 } 90 cell.contentlabel.text=uid; 91 cell.titilelabel.text=@"我的账号:"; 92 return cell; 93 } 94 } 95 else if([indexPath section]==1) 96 { 97 if([indexPath row]==0) 98 { 99 static NSString *modifyinfoTableIdentifier=@"LookInfoModelCell"; 100 infoCell *cell= (infoCell *)[tableView dequeueReusableCellWithIdentifier:modifyinfoTableIdentifier]; 101 if(cell==nil){ 102 NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"infoCell" owner:self options:nil]; 103 cell = [nib objectAtIndex:0]; 104 } 105 cell.contentlabel.text=sex; 106 cell.titilelabel.text=@"性别:"; 107 return cell; 108 } 109 else if([indexPath row]==1) 110 { 111 static NSString *modifyinfoTableIdentifier=@"LookInfoModelCell"; 112 infoCell *cell= (infoCell *)[tableView dequeueReusableCellWithIdentifier:modifyinfoTableIdentifier]; 113 if(cell==nil){ 114 NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"infoCell" owner:self options:nil]; 115 cell = [nib objectAtIndex:0]; 116 } 117 cell.contentlabel.text=address; 118 cell.titilelabel.text=@"地区:"; 119 return cell; 120 } 121 else 122 { 123 static NSString *modifyinfoTableIdentifier=@"LookInfoModelCell"; 124 infoCell *cell= (infoCell *)[tableView dequeueReusableCellWithIdentifier:modifyinfoTableIdentifier]; 125 if(cell==nil){ 126 NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"infoCell" owner:self options:nil]; 127 cell = [nib objectAtIndex:0]; 128 } 129 cell.contentlabel.text=gxqm; 130 cell.titilelabel.text=@"个性签名:"; 131 return cell; 132 } 133 134 } 135 else 136 { 137 static NSString *modifyinfoTableIdentifier=@"LookInfoModelCell"; 138 infoCell *cell= (infoCell *)[tableView dequeueReusableCellWithIdentifier:modifyinfoTableIdentifier]; 139 if(cell==nil){ 140 NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"infoCell" owner:self options:nil]; 141 cell = [nib objectAtIndex:0]; 142 } 143 cell.contentlabel.text=@"展示"; 144 cell.titilelabel.text=@"腾讯微博:"; 145 return cell; 146 147 } 148 149 }@end
最后就完成拉
如果还想实现其他效果的 话 就自定义相关的CELL样式 同时在不同条件下使用不同样式就可以了,具体的请参照如何实心新闻页面那一章