您的位置:首页 > 产品设计 > UI/UE

UITableView之一分组简单使用

2016-07-25 20:46 363 查看

分组也就是类似设置界面中的界面展示



1.首先在Storyboard中拖一个UITableView



2.其次设置分组属性默认是Plain我们选择Grouped



3.最后在将控件拖到代码中设置代理如下代码:

//
//  ViewController.m
//  TableView分组
//
//  Created by gaocai on 16/7/25.
//  Copyright © 2016年 gaocai. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
//设置代理
_tableView.dataSource = self;
}

#pragma mark - UITableViewDataSource代理方法

//显示多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 4;
}

//每一组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

if (section == 0) { //section组
return 2;
} else if (section == 1) {
return 3;
} else if (section == 2) {
return 4;
} else {
return 5;
}
}

//每一组的每一行都是一个UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [[UITableViewCell alloc] init];
NSInteger section = indexPath.section;
NSInteger row = indexPath.row;
if (section == 0) {
if (row == 0) {
cell.textLabel.text = @"通用";
} else if(row == 1) {
cell.textLabel.text = @"设置";
}
} else {

cell.textLabel.text = @"Other";
}
return cell;
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: