iOS:實現表格填充和選擇操作

來源:互聯網
上載者:User

標籤:des   style   blog   color   os   io   ar   for   2014   

 功能:建立一個列表,用數組填充表格,並支援挑選清單行

////  main.m//  Hello////  Created by lishujun on 14-8-28.//  Copyright (c) 2014年 lishujun. All rights reserved.//#import <UIKit/UIKit.h>// 視圖控制器對象@interface HelloWorldViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>{    NSArray *array;    UITableView *tableView;    //不用@property聲明,就不能用self進行引用? 有什麼差異,我得弄清楚}@end@implementation HelloWorldViewController-(HelloWorldViewController *) init{    array = [[NSArray alloc]initWithObjects:@"oops", @"hello", @"world", nil];    return self;}-(void) loadView{    // 建立視圖對象    UIView *contentView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];    contentView.backgroundColor = [UIColor lightGrayColor];    self.view = contentView;        // 建立tableView    tableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];    [tableView setDataSource:self];    [tableView setDelegate:self];        // tableView加入到內容視圖    [contentView addSubview:tableView];}// --------DataSource介面必須實現的方法,用於填充表格------------(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{    return 1; //設定列表分區個數}-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [array count]; //設定列表長度}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    // 產生列表單元(儘可能複用已有單元)    static NSString *simpleTableIdentifer = @"SimpleTableIdentifer";        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifer];    if (cell == nil) {        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifer];    }    cell.textLabel.text = array[indexPath.row];    return cell;}// ----------實現Delegate介面的方法,用於操作----------------(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{    // 行選中之前的事件,下面代碼實現是實現取消第一行的選擇,即不允許選擇    if(indexPath.row == 0)    {        return nil;    }    else    {        return indexPath;    }}-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    //行選中事件    NSString *rowValue = array[indexPath.row];    [tableView deselectRowAtIndexPath:indexPath animated:YES]; //取消選中狀態    NSLog(@"%@", rowValue);    }@end// 委派物件@interface HelloWorldAppDelegate : NSObject <UIApplicationDelegate>{    IBOutlet UIWindow *window;}@property (nonatomic, retain) UIWindow *window;@end@implementation HelloWorldAppDelegate@synthesize window;-(void) applicationDidFinishLaunching:(UIApplication *)application{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds]];    HelloWorldViewController *viewController = [[HelloWorldViewController alloc]init];    self.window.rootViewController = viewController;    [self.window makeKeyAndVisible];}@end// 程式入口int main(int argc, char * argv[]){    @autoreleasepool {        return UIApplicationMain(argc, argv, nil, @"HelloWorldAppDelegate");    }}

 

iOS:實現表格填充和選擇操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.