一稠曼、UITableView基本介紹
iOS開發(fā)中UITableView可以說是使用最廣泛的控件芥丧,我們平時(shí)使用的軟件中到處都可以看到它的影子刁绒,微信闷营、QQ、新浪微博等軟件基本都有UITableView知市。
UITableView有兩種style:
UITableViewStylePlain
UITableViewStyleGrouped傻盟。
UITableView中數(shù)據(jù)只有行的概念,并沒有列的概念嫂丙,因?yàn)樵谑謾C(jī)操作系統(tǒng)中顯示多列是不利于操作的娘赴。UITableView中每行數(shù)據(jù)都是一個(gè)UITableViewCell,在這個(gè)控件中為了顯示更多的信息跟啤,iOS已經(jīng)在其內(nèi)部設(shè)置好了多個(gè)子控件以供開發(fā)者使用诽表。如果我們查看UITableViewCell的聲明文件可以發(fā)現(xiàn)在內(nèi)部有一個(gè)UIView控件(contentView,作為其他元素的父控件)隅肥、兩個(gè)UILable控件(textLabel竿奏、detailTextLabel)、一個(gè)UIImage控件(imageView)腥放,分別用于容器泛啸、顯示內(nèi)容、詳情和圖片捉片。不同的UItableViewCell不同的類型:
typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
UITableViewCellStyleDefault, // 左側(cè)顯示textLabel(不顯示detailTextLabel)平痰,imageView可選(顯示在最左邊)
UITableViewCellStyleValue1, // 左側(cè)顯示textLabel汞舱、右側(cè)顯示detailTextLabel(默認(rèn)藍(lán)色)伍纫,imageView可選(顯示在最左邊)
UITableViewCellStyleValue2, // 左側(cè)依次顯示textLabel(默認(rèn)藍(lán)色)和detailTextLabel,imageView可選(顯示在最左邊)
UITableViewCellStyleSubtitle // 左上方顯示textLabel昂芜,左下方顯示detailTextLabel(默認(rèn)灰色),imageView可選(顯示在最左邊)
};
數(shù)據(jù)源dataSource
UITableView設(shè)置完dataSource后需要實(shí)現(xiàn)UITableViewDataSource協(xié)議莹规,在這個(gè)協(xié)議中定義了多種數(shù)據(jù)操作方法,創(chuàng)建一個(gè)簡單的聯(lián)系人管理進(jìn)行演示:
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface ContectUserInfo : NSObject
#pragma mark 姓
@property (nonatomic,copy) NSString *firstName;
#pragma mark 名
@property (nonatomic,copy) NSString *lastName;
#pragma mark 手機(jī)號碼
@property (nonatomic,copy) NSString *phoneNumber;
#pragma mark 帶參數(shù)的構(gòu)造函數(shù)
-(ContectUserInfo *)initWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName andPhoneNumber:(NSString *)phoneNumber;
#pragma mark 取得姓名
-(NSString *)getName;
#pragma mark 帶參數(shù)的靜態(tài)對象初始化方法
+(ContectUserInfo *)initWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName andPhoneNumber:(NSString *)phoneNumber;
@end
NS_ASSUME_NONNULL_END
#import "ContectUserInfo.h"
@implementation ContectUserInfo
-(ContectUserInfo *)initWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName andPhoneNumber:(NSString *)phoneNumber{
if(self=[super init]){
self.firstName=firstName;
self.lastName=lastName;
self.phoneNumber=phoneNumber;
}
return self;
}
-(NSString *)getName{
return [NSString stringWithFormat:@"%@ %@",_lastName,_firstName];
}
+(ContectUserInfo *)initWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName andPhoneNumber:(NSString *)phoneNumber{
ContectUserInfo *contact1=[[ContectUserInfo alloc]initWithFirstName:firstName andLastName:lastName andPhoneNumber:phoneNumber];
return contact1;
}
@end
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface ContectUserInfoGroup : NSObject
#pragma mark 組名
@property (nonatomic,copy) NSString *name;
#pragma mark 分組描述
@property (nonatomic,copy) NSString *detail;
#pragma mark 聯(lián)系人
@property (nonatomic,strong) NSMutableArray *contacts;
#pragma mark 帶參數(shù)個(gè)構(gòu)造函數(shù)
-(ContectUserInfoGroup *)initWithName:(NSString *)name andDetail:(NSString *)detail andContacts:(NSMutableArray *)contacts;
#pragma mark 靜態(tài)初始化方法
+(ContectUserInfoGroup *)initWithName:(NSString *)name andDetail:(NSString *)detail andContacts:(NSMutableArray *)contacts;
@end
NS_ASSUME_NONNULL_END
#import "ContectUserInfoGroup.h"
@implementation ContectUserInfoGroup
-(ContectUserInfoGroup *)initWithName:(NSString *)name andDetail:(NSString *)detail andContacts:(NSMutableArray *)contacts{
if (self=[super init]) {
self.name=name;
self.detail=detail;
self.contacts=contacts;
}
return self;
}
+(ContectUserInfoGroup *)initWithName:(NSString *)name andDetail:(NSString *)detail andContacts:(NSMutableArray *)contacts{
ContectUserInfoGroup *group1=[[ContectUserInfoGroup alloc]initWithName:name andDetail:detail andContacts:contacts];
return group1;
}
@end
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface ContactViewController : UIViewController
@end
NS_ASSUME_NONNULL_END
#import "ContactViewController.h"
#import "ContectUserInfo.h"
#import "ContectUserInfoGroup.h"
@interface ContactViewController ()<UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *contaceList;
@end
@implementation ContactViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"tableView分組數(shù)據(jù)源";
[self.view addSubview:self.tableView];
// Do any additional setup after loading the view.
}
#pragma mark - 數(shù)據(jù)源方法
#pragma mark 返回分組數(shù)
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSLog(@"計(jì)算分組數(shù)");
return [self.contaceList count];
}
#pragma mark 返回每組行數(shù)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSLog(@"計(jì)算每組(組%li)行數(shù)",(long)section);
ContectUserInfoGroup *group1=[self.contaceList objectAtIndex:section];
return group1.contacts.count;
}
#pragma mark返回每行的單元格
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//NSIndexPath是一個(gè)結(jié)構(gòu)體泌神,記錄了組和行信息
NSLog(@"生成單元格(組:%li,行%li)",(long)indexPath.section,(long)indexPath.row);
ContectUserInfoGroup *group=[self.contaceList objectAtIndex:indexPath.section];
ContectUserInfo *contact=group.contacts[indexPath.row];
UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.textLabel.text=[contact getName];
cell.detailTextLabel.text=contact.phoneNumber;
return cell;
}
#pragma mark 返回每組頭標(biāo)題名稱
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSLog(@"生成組(組%li)名稱",(long)section);
ContectUserInfoGroup *group=[self.contaceList objectAtIndex:section];
return group.name;
}
#pragma mark 返回每組尾部說明
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
NSLog(@"生成尾部(組%li)詳情",(long)section);
ContectUserInfoGroup *group=[self.contaceList objectAtIndex:section];
return group.detail;
}
- (NSMutableArray *)contaceList{
if (!_contaceList) {
_contaceList = [NSMutableArray array];
ContectUserInfo *contact1=[ContectUserInfo initWithFirstName:@"Cui" andLastName:@"Kenshin" andPhoneNumber:@"18500131234"];
ContectUserInfo *contact2=[ContectUserInfo initWithFirstName:@"Cui" andLastName:@"Tom" andPhoneNumber:@"18500131237"];
ContectUserInfoGroup *group1=[ContectUserInfoGroup initWithName:@"C" andDetail:@"With names beginning with C" andContacts:[NSMutableArray arrayWithObjects:contact1,contact2, nil]];
[_contaceList addObject:group1];
ContectUserInfo *contact3=[ContectUserInfo initWithFirstName:@"Lee" andLastName:@"Terry" andPhoneNumber:@"18500131238"];
ContectUserInfo *contact4=[ContectUserInfo initWithFirstName:@"Lee" andLastName:@"Jack" andPhoneNumber:@"18500131239"];
ContectUserInfo *contact5=[ContectUserInfo initWithFirstName:@"Lee" andLastName:@"Rose" andPhoneNumber:@"18500131240"];
ContectUserInfoGroup *group2=[ContectUserInfoGroup initWithName:@"L" andDetail:@"With names beginning with L" andContacts:[NSMutableArray arrayWithObjects:contact3,contact4,contact5, nil]];
[_contaceList addObject:group2];
ContectUserInfo *contact6=[ContectUserInfo initWithFirstName:@"Sun" andLastName:@"Kaoru" andPhoneNumber:@"18500131235"];
ContectUserInfo *contact7=[ContectUserInfo initWithFirstName:@"Sun" andLastName:@"Rosa" andPhoneNumber:@"18500131236"];
ContectUserInfoGroup *group3=[ContectUserInfoGroup initWithName:@"S" andDetail:@"With names beginning with S" andContacts:[NSMutableArray arrayWithObjects:contact6,contact7, nil]];
[_contaceList addObject:group3];
ContectUserInfo *contact8=[ContectUserInfo initWithFirstName:@"Wang" andLastName:@"Stephone" andPhoneNumber:@"18500131241"];
ContectUserInfo *contact9=[ContectUserInfo initWithFirstName:@"Wang" andLastName:@"Lucy" andPhoneNumber:@"18500131242"];
ContectUserInfo *contact10=[ContectUserInfo initWithFirstName:@"Wang" andLastName:@"Lily" andPhoneNumber:@"18500131243"];
ContectUserInfo *contact11=[ContectUserInfo initWithFirstName:@"Wang" andLastName:@"Emily" andPhoneNumber:@"18500131244"];
ContectUserInfo *contact12=[ContectUserInfo initWithFirstName:@"Wang" andLastName:@"Andy" andPhoneNumber:@"18500131245"];
ContectUserInfoGroup *group4=[ContectUserInfoGroup initWithName:@"W" andDetail:@"With names beginning with W" andContacts:[NSMutableArray arrayWithObjects:contact8,contact9,contact10,contact11,contact12, nil]];
[_contaceList addObject:group4];
ContectUserInfo *contact13=[ContectUserInfo initWithFirstName:@"Zhang" andLastName:@"Joy" andPhoneNumber:@"18500131246"];
ContectUserInfo *contact14=[ContectUserInfo initWithFirstName:@"Zhang" andLastName:@"Vivan" andPhoneNumber:@"18500131247"];
ContectUserInfo *contact15=[ContectUserInfo initWithFirstName:@"Zhang" andLastName:@"Joyse" andPhoneNumber:@"18500131248"];
ContectUserInfoGroup *group5=[ContectUserInfoGroup initWithName:@"Z" andDetail:@"With names beginning with Z" andContacts:[NSMutableArray arrayWithObjects:contact13,contact14,contact15, nil]];
[_contaceList addObject:group5];
}
return _contaceList;
}
- (UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
_tableView.backgroundColor = [UIColor whiteColor];
_tableView.dataSource=self;//設(shè)置數(shù)據(jù)源良漱,注意必須實(shí)現(xiàn)對應(yīng)的UITableViewDataSource協(xié)議
}
return _tableView;
}
@end
#pragma mark 返回每組標(biāo)題索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
NSLog(@"生成組索引");
NSMutableArray *indexs=[[NSMutableArray alloc]init];
for(ContectUserInfo *group in self.contaceList){
[indexs addObject:group.firstName];
}
return indexs;
}
計(jì)算分組數(shù)>>>計(jì)算每組的行數(shù)>>>生成分組索引>>>依次生成每組的單元格
代理方法
#pragma mark - 代理方法
#pragma mark 設(shè)置分組標(biāo)題內(nèi)容高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if(section==0){
return 50;
}
return 40;
}
#pragma mark 設(shè)置每行高度(每行高度可以不一樣)
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 45;
}
#pragma mark 設(shè)置尾部說明內(nèi)容高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 40;
}
點(diǎn)擊監(jiān)聽
#pragma mark 點(diǎn)擊行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
_selectedIndexPath=indexPath;
ContectUserInfoGroup *group=[self.contaceList objectAtIndex:indexPath.section];
ContectUserInfo *contact=group.contacts[indexPath.row];
//創(chuàng)建彈出窗口
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"System Info" message:[contact getName] delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
alert.alertViewStyle=UIAlertViewStylePlainTextInput; //設(shè)置窗口內(nèi)容樣式
UITextField *textField= [alert textFieldAtIndex:0]; //取得文本框
textField.text=contact.phoneNumber; //設(shè)置文本框內(nèi)容
[alert show]; //顯示窗口
}