IOS5基礎之十-----表視圖

一,一個簡單的表視圖

新建一個項目使用Single View Application模板並且命名爲Simple Table。

找到xib文件,將庫中的Table View 拖入到xib中,並且將連接檢查器中的datasource和Delegate 關聯到File‘s Owner上。保存這樣頁面的基本完成。

在頭文件中添加委託和資源。

#import<UIKit/UIKit.h>


@interface BIDViewController :UIViewController

<UITableViewDelegate,UITableViewDataSource>

@property (strong,nonatomic)NSArray *listData;


@end


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

   NSArray *array =[[NSArrayalloc]initWithObjects:@"sleepy",@"Sneezy",@"Bashful",@"Happy",@"Doc",

   @"Grumpy",@"Dopey",@"Thorin",@"Dorin",@"Nori",@"Ori",@"Balin",@"Dwalin",@"Fili",@"Kili",@"Oin",@"Gloin",@"Bifur",@"Bofur",@"Bomfur",nil];

    self.listData=array;

}


- (void)viewDidUnload

{

    [superviewDidUnload];

   // Release any retained subviews of the main view.

   // e.g. self.myOutlet = nil;

    self.listData =nil;

}


#pragma mark

#pragma mark Table View Data Source Methods

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

{

    //該方法來查看指定分區中的行數。

   return [self.listDatacount];

}


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

{

    static NSString *SimpleTableIdentifier=@"SimapleTableIdentifier";//此字符串充當表示某種表單元的鍵。

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

    if(cell==nil)

    {

        cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:SimpleTableIdentifier];

    }

    NSUInteger row =[indexPath row];

    cell.textLabel.text=[listDataobjectAtIndex:row];

     return cell;

}

簡單一個表視圖就完成了。

玩了兩天,今天開始學習,努力中!

現在爲每個單元格添加圖像。

也就是增加了一句話。

 UIImage *image=[UIImageimageNamed:@"star2.png"];

 cell.imageView.image=image;

每個單元格都有imageView和highlightedImage屬性。

如何設置單元格的詳細文本標籤。修改爲:

cell=[[UITableViewCellallocinitWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:SimpleTableIdentifier];


cell=[[UITableViewCellallocinitWithStyle:UITableViewCellStyleValue1 reuseIdentifier:SimpleTableIdentifier];

cell=[[UITableViewCellallocinitWithStyle:UITableViewCellStyleValue2reuseIdentifier:SimpleTableIdentifier];



設置縮進級別

#pragma mark

#pragma mark Table Delegate Methods

-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSInteger row =[indexPath row];

    return row;

}


處理行的選擇

選中第一行不做任何操作

-(NSIndexPath *) tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSUInteger row =[indexPath row];

    if(row==0)

        return nil;

    return indexPath;

}

選中其他行是彈出警告框

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSInteger row =[indexPath row];

    NSString *rowValue=[listData objectAtIndex:row];

    

    NSString *message= [[NSString allocinitWithFormat:@"You Selected %@",rowValue];

    UIAlertView *alert= [[UIAlertView allocinitWithTitle:@"Row Selected" message:message delegate:nil cancelButtonTitle:@"Yes I did"otherButtonTitles:nil];

    [alert show];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}


更改字體大小和行高

cell.textLabel.font=[UIFont boldSystemFontOfSize:50];

根據委託來修改行高

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

{

    return 70;

}





發佈了102 篇原創文章 · 獲贊 35 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章