UITableView和UITableViewcell

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;
}
發佈了65 篇原創文章 · 獲贊 3 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章