初始化
UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
属性
tableView.delegate = self;
tableView.dataSource = self;
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
headerView.backgroundColor = [UIColor greenColor];
tableView.tableHeaderView = headerView;
[headerView release];
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
footerView.backgroundColor = [UIColor brownColor];
tableView.tableFooterView = footerView;
[footerView release];
协议方法
#pragma mark -- dataSource 必须实现的2个方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:
UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];
static int number = 0;
number++;
NSLog(@"%d",number);
}
cell.textLabel.text = @"哈哈";
cell.detailTextLabel.text = @"呵呵";
cell.imageView.image = [UIImage imageNamed:@"4.jpg"];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
headView.backgroundColor = [UIColor grayColor];
return [headView autorelease];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
footView.backgroundColor = [UIColor purpleColor];
return [footView autorelease];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
switch (section) {
case 0:
return @"A";
break;
case 1:
return @"B";
break;
case 2:
return @"C";
break;
default:
return @"#";
break;
}
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSArray *sectionIndexTitles = @[@"A",@"B",@"C"];
return sectionIndexTitles;
}
tableView和数据
- (void)viewDidLoad {
[super viewDidLoad];
[self setUpData];
[self addTableView];
}
- (void)setUpData
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"TableViewPlist" ofType:@"plist"];
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
self.dataDic = [NSMutableDictionary dictionary];
NSArray *keys = [dic allKeys];
for (int i = 0; i < keys.count; i++) {
NSString *key = keys[i];
NSArray *value = dic[key];
NSMutableArray * tempArray = [NSMutableArray array];
for (NSDictionary *oneDic in value) {
CellModel *model = [[CellModel alloc] init];
[model setValuesForKeysWithDictionary:oneDic];
[tempArray addObject:model];
[model release];
}
[self.dataDic setObject:tempArray forKey:key];
}
}
- (void)addTableView
{
UITableView *tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
[tableView release];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:
UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];
}
NSArray *keys = self.dataDic.allKeys;
NSString *key = keys[indexPath.section];
NSArray *values = self.dataDic[key];
CellModel *model = values[indexPath.row];
cell.textLabel.text = model.title;
cell.imageView.image = [UIImage imageNamed:model.imageName];
return cell;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"sdf");
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *keys = self.dataDic.allKeys;
NSString *key = keys[section];
NSArray *value = self.dataDic[key];
return [value count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}