UITableView和UItableViewCell使用的很广泛,所以要熟练掌握。
布局如下:只创建了跟视图控制器,在跟视图控制器中编写UItableView和UItableViewCell。
重点:
1.使用数组和字典,务必要熟练掌握。因为以后联网了,基本都是存在数组或字典中。而且使用字典和数组可以大大简化代码
还是那句话,废话不多说,直接上代码
#import "RootViewController.h"
@interface RootViewController ()
@property(nonatomic,retain)UITableView * tableView;
@property(nonatomic,retain)NSArray * bigArray;
@property(nonatomic,retain)NSArray * array1;
@property(nonatomic,retain)NSArray * array2;
@property(nonatomic,retain)NSArray * array3;
@property(nonatomic,retain)NSArray * headerTittle;
@property(nonatomic,retain)NSMutableDictionary * dic;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStylePlain];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
self.array1 = @[@"通用",@"隐私"];
self.array2 = @[@"iCoud",@"地图",@"Safari",@"相机",@"GameCenter"];
self.bigArray = @[_array1,_array2];
self.headerTittle = @[@"设置",@"程序"];
NSArray *keyArray = @[@"一",@"二"];
self.dic = [NSMutableDictionary dictionaryWithObjects:_bigArray forKeys:keyArray];
// Do any additional setup after loading the view.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//每设置一个分组,访问一次本方法
//之所以可以写成下边一句,是因为section是变化的
// return [_bigArray[section] count];
#pragma mark -------------字典形式
NSArray *keys = [_dic allKeys];
NSString *key = [keys objectAtIndex:section];
NSArray *array = [_dic objectForKey:key];
return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
#pragma mark ===============cell的重用机制
//如果重用集合中,有可用的cell,则可以从中取出cell使用
//如果重用标示为@“cell”的对象的话,则取出使用
//如果重用集合中没有可用重用标识符为@“cell”的对象,则创建一个标示为@“cell”这样的一个新的cell对象
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]autorelease];
}
//创建cell,每设置一个cell,访问一次本方法
// UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
cell.imageView.image = [UIImage imageNamed:@"reg_logo.png"];
NSLog(@"%@",NSStringFromCGRect(cell.frame));
//设置显示的内容
// cell.textLabel.text = _bigArray[indexPath.section][indexPath.row];
//
// cell.detailTextLabel.text = _bigArray[indexPath.section][indexPath.row];
//
// return cell;
//用字典的形式显示内容
NSArray *keys = [_dic allKeys];
NSString *key = keys[indexPath.section];
NSArray *array = [_dic objectForKey:key];
cell.textLabel.text = array[indexPath.row];
return cell;
}
#pragma mark---------------实现UITableViewDelegate协议中的方法
//点击了那一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"secton = %lu,row = %lu",indexPath.section,indexPath.row);
}
//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = 100;
return height;
}
//设置区头的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30.0;
}
//设置区未的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 30.0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//返回几组,
return _bigArray.count;
}
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
NSString *str = _headerTittle[section];
return str;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return @[@"A",@"B",@"C"];
}
//自定义区未视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 30)];
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor redColor];
label.text = @"hahha";
return label;
}