我们通过tableviewController创建tableview,如果只是实现三个代理方法。我们看下效果
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) return 1;
if (section == 1) return 3;
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
switch (indexPath.section) {
case 0:
cell.textLabel.text = @"微信号";
cell.detailTextLabel.text = @"15901282931";
cell.accessoryType = UITableViewCellAccessoryNone;
return cell;
case 1:
if (indexPath.row == 0) {
cell.textLabel.text = @"QQ号";
cell.detailTextLabel.text = @"342934426";
return cell;
}else if (indexPath.row == 1) {
cell.textLabel.text = @"手机号";
cell.detailTextLabel.text = @"15901282931";
return cell;
}else {
cell.textLabel.text = @"邮箱地址";
cell.detailTextLabel.text = @"342934426@qq.com";
return cell;
}
default:
if (indexPath.row == 0) {
cell.textLabel.text = @"微信密码";
return cell;
}else if (indexPath.row == 1) {
cell.textLabel.text = @"账号保护";
BOOL isProtect = NO;
//创建 ImageView
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(240, 7, 30, 30)];
imageView.image = [UIImage imageNamed:isProtect ? @ "ProfileLockOn" : @"ProfileLockOff"];
//创建 Label
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(275, 2, 100, 40)];
label.text = isProtect ? @"以保护" : @"未保护";
label.font = [UIFont systemFontOfSize:14];
// label.textAlignment = NSTextAlignmentCenter;
[cell.contentView addSubview:imageView];
[cell.contentView addSubview:label];
return cell;
}else {
cell.textLabel.text = @"微信安全中心";
cell.detailTextLabel.text = @"342934426@qq.com";
return cell;
}
}
}
效果如下:
可以确定默认代码方式,tableview就是分组的形式。下面我们强行改变tableview的样式,再看一下效果
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
}
下面再通过,组头组尾的设置依然能够实现第一种效果:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 20;
}
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if (section != 2) return nil;
UIView *myView = [[UIView alloc]init];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, self.tableView.frame.size.width - 20, 40)];
label.text = @"大家快来送积分卡的数量咖啡就死定了疯狂就是对方的身份大家快来送积分卡的数量咖啡就死";
label.font = [UIFont systemFontOfSize:14];
label.textAlignment = NSTextAlignmentCenter;
label.numberOfLines = 2;
[myView addSubview:label];
return myView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 20;
}