1. 程式人生 > >純程式碼TableView例項

純程式碼TableView例項

 UITableView是app開發中常用到的控制元件,功能很強大,多用於資料的顯示。下面以一個簡單的例項來介紹tableview的基本用法。(適合新手,高手飄過)

@interface TableViewTestViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{

    UITableView *DataTable;

    NSMutableArray *dataArray1; //定義資料陣列1

    NSMutableArray *dataArray2;//定義資料陣列2

    NSMutableArray *titleArray;//定義標題陣列

}

- (void)viewDidLoad

{

    [superviewDidLoad];

//初始化tableview

DataTable = [[UITableViewallocinitWithFrame:CGRectMake(00320420)];//指定位置大小

    [DataTablesetDelegate:self];//指定委託

    [DataTablesetDataSource:self];//指定資料委託

    [self.viewaddSubview:

DataTable];//載入tableview

dataArray1 = [[NSMutableArrayallocinitWithObjects:@"中國"@"美國"@"英國"nil];//初始化資料陣列1

dataArray2 = [[NSMutableArrayallocinitWithObjects:@"黃種人"@"黑種人"@"白種人"nil];//初始化資料陣列2

titleArray = [[NSMutableArrayallocinitWithObjects:@"國家"@"種族"nil];//初始化標題陣列

}

- (BOOL

)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

// Return YES for supported orientations

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

//每個section顯示的標題

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

    switch (section) {

        case 0:

            return [titleArray objectAtIndex:section];//提取標題陣列的元素用來顯示標題

        case 1:

            return [titleArray objectAtIndex:section];//提取標題陣列的元素用來顯示標題

        default:

            return @"Unknown";

    }

}

//指定有多少個分割槽(Section),預設為1

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

return [titleArray count];//返回標題陣列中元素的個數來確定分割槽的個數

}

//指定每個分割槽中有多少行,預設為1

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

    switch (section) {

        case 0:

           return  [dataArray1 count];//每個分割槽通常對應不同的陣列,返回其元素個數來確定分割槽的行數

            break;

        case 1:

            return  [dataArray2 count];

            break;

        default:

            return 0;

            break;

    }

}

//繪製Cell

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

    static NSString *CellIdentifier = @"Cell";

 //初始化cell並指定其型別,也可自定義cell

UITableViewCell *cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier];

  if(cell == nil

  {

  cell = [[[UITableViewCellalloc

  initWithFrame:CGRectZero 

  reuseIdentifier:CellIdentifier] autorelease];

}

   switch (indexPath.section) {

  case 0://對應各自的分割槽

    [[cell textLabel]  setText:[dataArray1 objectAtIndex:indexPath.row]];//給cell新增資料

    break;

  case 1:

    [[cell textLabel]  setText:[dataArray2 objectAtIndex:indexPath.row]];

    break;

  default:

    [[cell textLabel]  setText:@"Unknown"];

}

  return cell;//返回cell

}

tableview還有很多高難度的屬性和介面,在以後我會慢慢補齊。

上面的例子在功能上介紹了tableview的使用,但其在資料處理上具有很大的侷限性。當我們要從伺服器上請求資料,面對多種可能的資料(主要指陣列的個數不穩定)此時上面的switch將無法滿足我們的需求了。

使用switch可是程式碼的結構清晰明瞭,但其侷限性很致命(switch中case的個數無法動態指定),下面的另一種方法可解決上述問題。

程式碼在原由基礎上進行的修改:

@interface TableViewTestViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{

    UITableView *DataTable;

    NSMutableArray *dataArray1;

    NSMutableArray *dataArray2;

    NSMutableArray *titleArray;

    NSMutableArray *dataArray; //加入了用於儲存陣列的陣列 dataArray

}

- (void)viewDidLoad

{

    [superviewDidLoad];

DataTable = [[UITableViewallocinitWithFrame:CGRectMake(00320420)];

    [DataTablesetDelegate:self];

    [DataTablesetDataSource:self];

    [self.viewaddSubview:DataTable];

dataArray1 = [[NSMutableArrayallocinitWithObjects:@"中國"@"美國"@"英國"nil];

dataArray2 = [[NSMutableArrayallocinitWithObjects:@"黃種人"@"黑種人"@"白種人"nil];

titleArray = [[NSMutableArrayallocinitWithObjects:@"國家"@"種族"nil];

dataArray = [[NSMutableArrayallocinitWithObjects:dataArray1dataArray2nil]; //初始化dataArray,元素為陣列

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

// Return YES for supported orientations

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

 //制定個性標題,這裡通過UIview來設計標題,功能上豐富,變化多。

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

UIView *view = [[[UIViewallocinitWithFrame:CGRectMake(0032040)] autorelease];

    [view setBackgroundColor:[UIColorbrownColor]];//改變標題的顏色,也可用圖片

UILabel *label = [[UILabelallocinitWithFrame:CGRectMake(5510030)];

    label.textColor = [UIColorredColor];

    label.backgroundColor = [UIColorclearColor];

    label.text = [titleArrayobjectAtIndex:section];

    [view addSubview:label];

     return view;

}

 //指定標題的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

return 40;

}

//每個section顯示的標題,有了上面的這個就不要了

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

}

//指定有多少個分割槽(Section),預設為1

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

return [titleArraycount];

}

//指定每個分割槽中有多少行,預設為1

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

/* switch (section) {

        case 0:

           return  [dataArray1 count];

            break;

        case 1:

            return  [dataArray2 count];

            break;

        default:

            return 0;

            break;

    }*/

/*  for(int i = 0; i < [titleArray count]; i++){

        if(section == i){

            return [[dataArray objectAtIndex:section] count];

        }

    }*/

  //上面的方法也是可行的,大家參考比較下

return [[dataArray objectAtIndex:section] count];  //取dataArray中的元素,並根據每個元素(陣列)來判斷分割槽中的行數。

}

//繪製Cell

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

    static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = (UITableViewCell*)[tableView 

                                               dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil

{

cell = [[[UITableViewCellalloc

initWithFrame:CGRectZero 

reuseIdentifier:CellIdentifier] autorelease];

}

/*switch (indexPath.section) {

case 0:

[[cell textLabel] 

setText:[dataArray1 objectAtIndex:indexPath.row]];

break;

case 1:

[[cell textLabel] 

setText:[dataArray2 objectAtIndex:indexPath.row]];

break;

default:

[[cell textLabel] 

setText:@"Unknown"];

}*/

    //上面的方法也可行,大家比較下

    [[cell textLabelsetText:[[dataArrayobjectAtIndex:indexPath.section]objectAtIndex:indexPath.row]];

 //同上,取出dataArray中每個分割槽所對應的元素(陣列),並通過其來取值。 (大家要有想像力, 複製程式碼試試就明白了)

return cell;

}

 //改變行的高度

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

return40;

}

轉自:http://www.cnblogs.com/top5/archive/2012/05/17/2506604.html