我們可以看到顿天,在iOS開(kāi)發(fā)中堂氯,最常用的視圖之一就是UITableview,如果可以把這個(gè)視圖玩6了牌废,其實(shí)好多的界面設(shè)計(jì)通過(guò)該視圖就可以完成咽白,下面是我總結(jié)一下,該視圖常用的代理方法
1.第一步
該視圖所在的控制器要遵守這兩個(gè)協(xié)議 <UITableViewDataSource , UITableViewDelegate>
2.第二步
聲明該變量
@property (nonatomic , strong)UITableView *addressTableView;
3.第三步
實(shí)例化鸟缕,該視圖對(duì)象晶框,并進(jìn)行設(shè)置
#pragma mark UITableviewDatasource
//獲取該視圖的組數(shù)
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
}
//獲取每組有多少行數(shù)據(jù) row-行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
}
//索引表內(nèi)容-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{//這兒返回的是你索引里的數(shù)據(jù)組合
}
//索引表與組之間的關(guān)聯(lián)
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
//告訴我們一個(gè)段名和該段的序號(hào)
//我們需要返回一個(gè)對(duì)于索引表數(shù)組內(nèi)容的序號(hào)
NSInteger sectionCount= x;x是個(gè)未知數(shù),代表第幾組
for (NSString *aAlpha in _sectionNameArray) {//_sectionNameArray代表數(shù)據(jù)對(duì)應(yīng)的真實(shí)索引組合
if ([aAlpha isEqualToString:title]) {//title代表此刻右邊索引懂从,你所點(diǎn)擊的那一個(gè)
return sectionCount;
}
sectionCount++;
}
return sectionCount;//返回你點(diǎn)擊的那個(gè)索引在幾組數(shù)據(jù)中對(duì)應(yīng)的組頭授段,對(duì)應(yīng)的那個(gè)組數(shù)
}
//為cell賦值
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//這個(gè)方法為cell賦值,大概樣子如下
static NSString *CellIdentifier = @"userCell";
NTAdressBookCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell){
cell = [[NTAdressBookCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.nameLab.text = oneUser.nickname;
cell.JobLab.text = oneUser.jobTitle;
cell.departLab.text = oneUser.department;
return cell;
}
//下面是選中某個(gè)cell執(zhí)行的方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
//設(shè)置每個(gè)cell的高度莫绣,這個(gè)其實(shí)我們可以精確到某個(gè)組的某個(gè)row
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
}
//這個(gè)方法畴蒲,我們可以為該視圖的每個(gè)組設(shè)置一個(gè)標(biāo)題,主要是对室,通訊錄的a-z,缺點(diǎn)咖祭,不能自定義位置掩宜,大小
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
}
//因?yàn)橐陨戏椒ǖ亩贪澹覀兛梢杂米远x組頭視圖
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
}
//有自定義組頭視圖么翰,就有自定義組尾視圖
- ( UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
}
//組頭高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
}
//組尾高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
}