UITableView

插入行。刪除行。折疊區。設置索引。刷新表格。注冊cell。


//UITableView的ADD

// NSString * name = @"new";

//NSInteger section = indexPath.section;

NSMutableArray * newArray = [dataArray objectAtIndex:indexPath.section];

[newArray addObject:dataArray [indexPath.section] [indexPath.row]];

//NSInteger row =? newArray.count - 1;

NSIndexPath * path = [NSIndexPath indexPathForRow:newArray.count- 1 inSection:indexPath.section];

[tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationFade];

//UITableView的DELETE

NSMutableArray * lastArray =dataArray [indexPath.section];

if(lastArray.count> 0) {

[lastArray removeObjectAtIndex:indexPath.row];

}

[tableView reloadData];

UITableView的cell折疊

#import"ViewController.h"

#import"customTableViewCell.h"

@interfaceViewController() {

UITableView * table;

BOOL flag[3];

}

@end

@implementation ViewController

//釋放全局變量

-(void)dealloc {

[table release];

//繼承父類

[super dealloc];

}

- (void)viewDidLoad {

[super viewDidLoad];

//全局的UITableView

table = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStyleGrouped];

//UITableView代理

table.delegate = self;

table.dataSource = self;

//第二種注冊方式記得創建繼承于UITableViewCell的類下面創建cell時要注意類名稱

[table registerClass:[customTableViewCell class] forCellReuseIdentifier:@"cell"];

//系統自動偏移屬性

self.automaticallyAdjustsScrollViewInsets = NO;

//添加

[self.view addSubview:table];

}

//返回區

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 3;

}

//返回行

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

//默認折疊開關是關閉狀態

if(flag[section] == NO) {

//關閉狀態下行數為0

return 0;

} else {

//點擊打開后行數為5

return 5;

}

}

//重用機制方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//出列由于使用注冊所以不需要判斷if(!cell)

customTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

//行文本內容

cell.textLabel.text= [NSString stringWithFormat:@"%ld--%ld",indexPath.section,indexPath.row];

//行最右配件

cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;

return cell;

}

//區頭名稱可不寫

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

return @"區頭";

}

//區頭加載視圖

-(UIView *)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section {

//創建視圖不要寫添加return view;這一句就是默認添加

UIView * view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)] autorelease];

//寫button

UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

btn.frame = CGRectMake(0, 0, 44, 44);

//綁定方法要傳參

[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

//設置tag值

btn.tag = section + 10 ;

[view addSubview:btn];

//[btn release];不寫否則運行時系統會崩潰

//設置圖片視圖

UIImageView * imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"a"]];

imgView.frame = CGRectMake(0, 0, 44, 44);

[view addSubview:imgView];

[imgView release];

//設置圖片視圖的動畫

if(flag[section] ==NO) {

imgView.transform = CGAffineTransformIdentity;

} else {

imgView.transform = CGAffineTransformMakeRotation(M_PI_2);

}

return view;

}

-(void)btnClick:(UIButton *)btn {

//檢驗測試用的輸出可不寫

NSLog(@"btnClick");

NSLog(@"%ld",btn.tag);

//點擊取反按鈕狀態

flag[btn.tag- 10] = !flag[btn.tag- 10];

//檢驗測試用的輸出可不寫

NSLog(@"%ld",btn.tag);

//NSIndexSet:索引的集合其中的參數是想要刷新的區的集合

//用區創建一個集合集合的元素就是區號012

NSIndexSet * set = [NSIndexSet indexSetWithIndex:btn.tag- 10];

//行的動畫

[table reloadSections:set withRowAnimation:UITableViewRowAnimationFade];

}

@end


UITableView的索引條索引

-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {

return index;

}

//索引標題方法返回的是索引標題數組

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

NSMutableArray * array = [NSMutableArray array];

for(int i = 0; i < 6; i ++) {

NSString * str = [NSString stringWithFormat:@"第%d區",i];

[array addObject:str];

}

return array;

}


UITableView的刷新

//刷新表格3種形式

//1.[table reloadData];全部刷新刷新整個表

//2.局部刷新Sections:要刷新的區索引的集合

NSIndexSet * set = [NSIndexSet indexSetWithIndex:btn.tag-100];

//局部刷新區(NSIndexSet *)sections要刷新的區索引的集合

[table reloadSections:set withRowAnimation:UITableViewRowAnimationFade];

//3.局部刷新行(row)

//? ? NSIndexPath * path = [NSIndexPath indexPathForRow:2 inSection:0];

//? ? NSArray * array = @[path];

//? ? [table reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];


注冊cellUITableView

//注冊創建cell

//1.注冊系統的cell類

//[UILabel class];

//[UIView class];

//[table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

//2.注冊自定義類的

//[table registerClass:[customTableViewCell class] forCellReuseIdentifier:@"cell"];

//3.注冊Xib? cell

[tableregisterNib:[UINibnibWithNibName:@"XibTableViewCell"bundle:nil]forCellReuseIdentifier:@"cell"];

注冊cell的話不需要判斷

if(!cell){

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

}

UITableView

//UITableView:表格

UITableView * table = [[UITableViewalloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];

table.backgroundColor = [UIColor orangeColor];

//UITableView代理

table.delegate = self;

//數據源代理data ?

table.dataSource = self;

[self.view addSubview:table];

#pragma mark ====數據源代理

//返回多少行Section區參數區里有多少行

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

if(section ==0) {

return 6;

} else {

//返回值方法返回多少行

return 10;

}

}

//返回cell每一行單元格

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//創建cell

//dequeue:隊列

//返回cell的時候,先從重用隊列中找帶有重用標識的cell(可重用的cell)

UITableViewCell * cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell"];

//如果重用隊列里沒有那么就去創建一個返回cell

if(!cell1) {

cell1 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"cell1"];

}

//indexPath二維坐標indexPath.section:區(沒有設置的情況下,默認返回一個0區) indexPath.row:行

//默認44高度

//cell.textLabel.text = @"單元格";

cell1.textLabel.text= [NSString stringWithFormat:@"%d----%d",indexPath.section,indexPath.row];

returncell1;

}

//返回多少個區

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return2;

}

//TableView代理方法返回行高

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

//設置各個區不等高

if(indexPath.section==0) {//0區

switch(indexPath.row) {

case0:

case1:

return100;

break;

case2:

case3:

return200;

break;

default:

return44;

break;

}

}else{//1區

return44;

}

}

//點擊行觸發的方法

-(void)tableView:(nonnullUITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {

//indexPath索引路徑

NSLog(@"點擊cell----%d區%d行",indexPath.section,indexPath.row);

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容