iOS UITableView (類似Android的ListView)(一)

學習iOS有幾天了,今天看視頻學到了一個UITableView控件,效果和Android的listview類似,不過封裝了許多東西,比Android的listview強大多了,廢話少說,開始下手。

首先,拖入一個TableView,Style改爲Grouped分組

然後在ViewController.h中

接下來,開始我們ViewController.m的代碼編寫:

//
//  ViewController.m
//  UITableView-二維數組
//
//  Created by crw on 14-11-4.
//  Copyright (c) 2014年 crw. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource>{
    NSArray *_allCities;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化數據
    _allCities=@[
                @[@"1"],
                @[@"21",@"22"],
                @[@"31",@"32",@"33"]
    ];
    _tableView.dataSource=self;
}

#pragma mark 一共有多少組
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _allCities.count;
}
#pragma mark 第section組有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //第幾組的總行數
    return [_allCities[section] count];
}
#pragma mark 每一行的樣式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil ];
    //indexPath.section第幾組  indexPath.row第幾行
    cell.textLabel.text=_allCities[indexPath.section][indexPath.row];
    return cell;
}



@end
看下效果:


接下來給分組加上標題:

#pragma mark 每一組的標題
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"aa";
}
看看效果

當然,還可以加上尾部的說明

#pragma mark 每一組的尾部說明
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return @"footer";
}

看看效果:


最後,用面向對象的思想將代碼進行封裝,降低耦合:

ViewControl。m

//
//  ViewController.m
//  UITableView-二維數組
//
//  Created by crw on 14-11-4.
//  Copyright (c) 2014年 crw. All rights reserved.
//

#import "ViewController.h"
#include "Province.h"

@interface ViewController ()<UITableViewDataSource>{
    NSArray *_allCities;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //封裝了頭部標題,尾部說明,填充數據的類
    Province *first  = [Province provinceWithHeader:@"第一組" Footer:@"第一組說明" Cities:@[@"1",@"11"]];
    Province *second = [Province provinceWithHeader:@"第二組" Footer:@"第二組說明" Cities:@[@"2",@"22"]];
    Province *third  = [Province provinceWithHeader:@"第三組" Footer:@"第三組說明" Cities:@[@"3",@"33"]];
    
    //初始化數據
    _allCities=@[first,second,third];
    _tableView.dataSource=self;
}

#pragma mark 一共有多少組
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _allCities.count;
}

#pragma mark 第section組有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //第幾組的總行數
    return [[_allCities[section] cities] count];
}

#pragma mark 每一行的樣式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil ];
    //indexPath.section第幾組  indexPath.row第幾行
    cell.textLabel.text=[_allCities[indexPath.section] cities][indexPath.row];
    
    return cell;
}

#pragma mark 每一組的頭部標題
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [_allCities[section] header];
}

#pragma mark 每一組的尾部說明
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return [_allCities[section] footer];
}


@end

Province.h

//
//  Province.h
//  UITableView-二維數組
//
//  Created by crw on 14-11-5.
//  Copyright (c) 2014年 crw. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Province : NSObject
@property(nonatomic,copy)NSString *header;
@property(nonatomic,copy)NSString *footer;
@property(nonatomic,strong)NSArray *cities;
+(id)provinceWithHeader:(NSString *)header Footer:(NSString *)footer Cities:(NSArray *)cities;
@end

Province.m

//
//  Province.m
//  UITableView-二維數組
//
//  Created by crw on 14-11-5.
//  Copyright (c) 2014年 crw. All rights reserved.
//

#import "Province.h"

@implementation Province
+(id)provinceWithHeader:(NSString *)header Footer:(NSString *)footer Cities:(NSArray *)cities{
    Province *p=[[Province alloc] init];
    p.header=header;
    p.footer=footer;
    p.cities=cities;
    return p;
}
@end


最終效果


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章