iOS tableview用法

經過兩種方法來實現: 數組

1、經過動態數組NSMutableArray中的數據,來顯示數據app

 

1.新建Empty Application項目,新建ViewController,HomeViewController,在AppDelegate.m中導入該文件,ide

並在方法- (BOOL)application:didFinishLaunchingWithOptions:中添加如下紅色標記的代碼字體

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptionsatom

{spa

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];.net

    // Override point for customization after application launch.orm

    self.window.backgroundColor = [UIColor whiteColor];對象

    

    HomeViewController *homeViewController = [[HomeViewController alloc] init];索引

    self.window.rootViewController = homeViewController;

    [homeViewController release];

    

    [self.window makeKeyAndVisible];

    return YES;

 

2.在 HomeViewController.xib上添加Table View控件

將其Outlets的dataSource和delegate與File's Owner創建關聯,

 目的:(1) dataSource: 向HomeViewController添加UITableViewDataSource協議,從而能夠在該類中使用相關的協議方法,在Table View中顯示數據。 

         (2) delegate :向HomeViewController添加UITableViewDelegate協議,從而能夠在該類中使用相關的協議方法,響應用戶在Table View中的交互操做。

 

 在HomeViewController.h中添加協議

#import <UIKit/UIKit.h>

@interface HomeViewController : UIViewController

<UITableViewDelegate, UITableViewDataSource>{

}

@end

目的:都添加協議,有備無患。提升代碼編寫的效率和可靠性。

 

3. 在HomeViewController.m中編寫代碼

 

#import "HomeViewController.h"

@interface HomeViewController ()

@end

@implementation HomeViewController

NSMutableArray *listOfContacts;//聲明動態數組

- (void)viewDidLoad

{

 

    listOfContacts = [[NSMutableArray alloc] init];//分配內存並初始化

    

    [listOfContacts addObject:@"張三"];

    [listOfContacts addObject:@"張1"];

    [listOfContacts addObject:@"張2"];

    [listOfContacts addObject:@"張3"];

    [listOfContacts addObject:@"張4"];

    [listOfContacts addObject:@"張5"];

    [listOfContacts addObject:@"張6"];

    [listOfContacts addObject:@"張7"];

    [listOfContacts addObject:@"張8"];

    [listOfContacts addObject:@"張9"];

    [listOfContacts addObject:@"張11"];

    [listOfContacts addObject:@"張12"];

    [listOfContacts addObject:@"張13"];

    [listOfContacts addObject:@"張14"];

    [listOfContacts addObject:@"張15"];

    [listOfContacts addObject:@"張16"];

    [listOfContacts addObject:@"張17"];

    [listOfContacts addObject:@"張18"];

    [listOfContacts addObject:@"張19"];

    

    [super viewDidLoad];

}

//使用UITableViewDataSource協議的tableView:cellForRowAtIndexPath:方法

//該方法用來將數據填充進Table View的單元格中

/*

 在Table View中每填充一個單元格的數據就會觸發一次該事件。

 注意:若是咱們的數據一共有200項,並不表明會連續觸發200次這個事件,若是當前屏幕只能顯示10行數據的話,就只會觸發10次該事件,當用戶滾動該Table View而產生新的單元格時,纔會繼續觸發該事件。

 */

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

    

    static NSString *CellIndentifier = @"Contact";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];

    /*

     dequeueReusableCellWithIdentifier方法,獲取UITableViewCell類型的對象,並且獲取的是一個已經在TableView中使用過並能夠複用的對象。

     想象一下:

     若是數組中有1000個元素,咱們爲每個元素都實例化一個UITableViewCell對象的話,系統就會內存溢出甚至崩潰。其實每一個用戶在一個屏幕中可以看到的單元格數量也就十幾個,他們經過上下滾動屏幕的操做可讓一些已顯示的單元格消除,而這些單元格對象系統就會保留下來以備咱們須要顯示新單元格時能夠複用它們,從而達到了節省系統資源的目的。這個方法包含一個參數CellIdentifier, 它用於指明你須要哪一個標識的可複用單元格。在同一界面中若是有多個表格的狀況時很是有用。

     固然若是沒有獲取到可複用的單元格時,咱們就須要使用UITableViewCell的initWithStyle:reuseIdentifier:方法直接實例化一個單元格。其中reuseIdentifier參數用於設置該表格的可複用標識。

     */

    

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier] autorelease];

        

    }

    

    NSString *cellValue = [listOfContacts objectAtIndex:indexPath.row];

    cell.textLabel.text = cellValue;

    

    //示意標誌: Disclosure Indicator,Disclosure Button,Checkmark,默認爲None

    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    //cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

    //cell.accessoryType = UITableViewCellAccessoryCheckmark;

    cell.accessoryType = UITableViewCellAccessoryNone;

    

    //單元格添加圖片

    UIImage *image = [UIImage imageNamed:@"avatar.png"];

    cell.imageView.image = image;

    

    return cell;

}

//使用UITableViewDataSource協議的tableView:numberOfRowsInSection:方法

//該方法用來設置Table View中要顯示數據的行數

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

    return [listOfContacts count];

}

//添加標題和腳本信息

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

      return @"聯繫人列表";

    

}

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

    return @"做者:what if";

}

 

 

 

 //UITableViewDelegate協議的方法,選擇表格中的項目

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

    NSString *contactSelected = [listOfContacts objectAtIndex:[indexPath row]];

    NSString *msg = [[NSString alloc] initWithFormat:@"您選擇的聯繫人:%@", contactSelected];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"選擇聯繫人" message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alert show];

    [alert release];

    [contactSelected release];

    [msg release];

 //UITableViewDelegate協議的方法,表格中的縮進

- (NSIntegertableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{

    return [indexPath row] % 9;

    

}

- (void)dealloc{

 

    [listOfContacts release];

    [super dealloc];

    

}

- (void)viewDidUnload

{

    [super viewDidUnload];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

@end

 

2、經過plist文件提供數據,來顯示數據,方便分組

 1.添加contacts.plist文件

 2. 

HomeViewController.h中添加代碼

#import <UIKit/UIKit.h>

@interface HomeViewController : UIViewController

<UITableViewDelegate, UITableViewDataSource>{

}

@property (nonatomic, retain) NSDictionary *contactTitles;//存儲全部的聯繫人信息

@property (nonatomic, retain) NSArray *groups;//全部分類名稱存入數組中

@end 

 

3HomeViewController.m中添加代碼 


#import "HomeViewController.h"

@interface HomeViewController ()

@end

@implementation HomeViewController

@synthesize contactTitles;

@synthesize groups;

- (void)viewDidLoad

{

   

    NSString *path = [[NSBundle mainBundle] pathForResource:@"contacts" ofType:@"plist"];//plist文件路徑

    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];

    self.contactTitles = dict;

    [dict release];

    

    NSArray *array = [[contactTitles allKeys] sortedArrayUsingSelector:@selector(compare:)];

    

    self.groups = array;

    [super viewDidLoad];

}

//使用UITableViewDataSource協議的tableView:cellForRowAtIndexPath:方法

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

    

    static NSString *CellIndentifier = @"Contact";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];

    

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIndentifier] autorelease];

        

    }

    

    

       NSString *group = [groups objectAtIndex:[indexPath section]];

NSArray * contactSection = [contactTitles objectForKey:group];

cell.textLabel.text = [contactSection objectAtIndex:[indexPath row]];

    

    //單元格添加圖片

    UIImage *image = [UIImage imageNamed:@"avatar.png"];

    cell.imageView.image = image;

    

    

    return cell;

}

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

    return [groups count];

}

//使用UITableViewDataSource協議的tableView:numberOfRowsInSection:方法

//該方法用來設置Table View中要顯示數據的行數

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

 

    NSString *group = [groups objectAtIndex:section];

    NSArray *contactSection = [contactTitles objectForKey:group];

    

    return [contactSection count];

}

//添加標題和腳本信息

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

    NSString *group = [groups objectAtIndex:section];

    return group;

    

}

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

    return @"做者:what if";

}


 

 

 /*//UITableViewDelegate協議的方法,選擇表格中的項目

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

    NSString *contactSelected = [listOfContacts objectAtIndex:[indexPath row]];

    NSString *msg = [[NSString alloc] initWithFormat:@"您選擇的聯繫人:%@", contactSelected];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"選擇聯繫人" message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alert show];

    [alert release];

    [contactSelected release];

    [msg release];

} */

/*

 //UITableViewDelegate協議的方法,表格中的縮進

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

    return [indexPath row] % 9;

    

}*/

//索引功能 

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

    return groups;

}

//用戶點擊標誌後觸發的事件,只有DetailDisclosure Button纔有該事件

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{

    //進入到該項目的詳細信息頁面

 

 

- (void)dealloc{

    

    [contactTitles release];

    [groups release];

    [super dealloc];

    

}

- (void)viewDidUnload

{

    [super viewDidUnload];

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

@end

 

4.在xib文件中修改其Style爲Grouped


 備註:

修改單元格的方法:

textLabel.font  使用UIFont設置單元格標籤的字體

textLabel.lineBreakMode   使用UILineBreakMode指定單元格標籤的文本如何換行

textLabel.text   將單元格標籤的內容設置爲一個NSString

textLabel.textAlignment   使用UITextAlignment設置單元格標籤文本的對齊方式

textLabel.textColor    使用UIColor設置單元格標籤文本的顏色

textLabel.selectedTextColor   使用UIColor設置選定文本的顏色

imageView.image   將單元格的圖像視圖設置爲一個UIImage

imageView.selectedImage   將選定單元格的內容設置爲UIImage

相關文章
相關標籤/搜索