//在网上找了一些表格视图,简单明了的代码不多,自己贴一个上来,有空研究一下。
@interface ViewController () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSMutableArray *dataArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self getData];
[self createTableView];
NSString *home = NSHomeDirectory();
NSLog(@"--=--%@",home);
}
- (void)getData{
// 班级中,10个分组,每个分组10个人 --->100个数据
_dataArray = [[NSMutableArray alloc] init];
for (int i=0; i<10; i++) {
NSMutableArray *mAarray = [[NSMutableArray alloc] init];
for (int j=0; j<20; j++) {
NSString *str = [NSString stringWithFormat:@"IOS1505 第%d组中第%d个学生",i+1,j+1];
[mAarray addObject:str];
}
[_dataArray addObject:mAarray];
}
}
- (void)createTableView{
UITableView *table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
table.dataSource = self;
table.delegate = self;
[self.view addSubview:table];
}
// 每组中的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *array = _dataArray[section];
return array.count;
}
// 分组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _dataArray.count;
}
// 单元格详细内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
// 一个屏幕14单元格 ---》 创建15个单元格
// cell的复用
static NSString *cellID = @"QF";
// 去缓存池中找
static int num = 0;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
num ++;
NSLog(@"num:%d",num);
}
cell.textLabel.text = @"标题";
cell.detailTextLabel.text = _dataArray[indexPath.section][indexPath.row];
// if (indexPath.section == 1) {
// if (indexPath.row == 2) {
// cell.textLabel.text = @"--------";
// cell.detailTextLabel.text = @"------------";
// }
// }
// 专门有个方法处理
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton ;
if (indexPath.row == 1) {
// cell.backgroundColor = [UIColor redColor];
}
return cell;
}
// 分组标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (section == 0) {
return @"第一组";
}
if (section == 1) {
return @"第二组";
}
if (section == 2) {
return @"第三组";
}
return @"其他";
}
// 分组脚注
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
return @"脚注";
}
// 右边索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
// 注意:和分组标题没有任何关系,只和分组位置有关
// return @[@"a",@"b",@"c"];
// return @[@"test",@"haha",@"hehe",@"xixi"];
NSMutableArray *mArray = [[NSMutableArray alloc] init];
for (int i='A'; i<='Z'; i++) {
[mArray addObject:[NSString stringWithFormat:@"%c",i]];
}
return mArray;
}
// 单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 1) {
return 100;
}
return 44;
}
// 分组标题行高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 200;
}
// 分组脚注行高
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 100;
}
// 分组标题的UIView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
if (section == 3) {
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:@"image0"];
return imageView;
}
return nil;
}
// 分组脚注UIView
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
if (section == 1) {
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:@"image0"];
return imageView;
}
return nil;
}
// 选中某行的代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UIAlertView *alert = [[UIAlertView alloc] init];
alert.title = @"提示";
NSString *str = [NSString stringWithFormat:@"第%d组-第%d个学生",indexPath.section+1,indexPath.row+1];
alert.message = str;
[alert addButtonWithTitle:@"确定"];
[alert show];
}
// 单元格右边感叹号
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
UIAlertView *alert = [[UIAlertView alloc] init];
alert.title = @"错误提示";
alert.message = @"错误!!!!!!!!!!!!!!!!";
[alert addButtonWithTitle:@"确定"];
[alert show];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end