iOS UI 选择!列表式 UITableView

选择!列表式 UITableView

UITableView 是继承UISCrollView后再搭配一至多个UILabel所组成的,它能提供列表的资料显示方式,以下为最基本的UITableView示范。

执行结果

Log

已选择的cell编号: 2
cell值: 资料第三项

已选择的cell编号: 3
cell值: 资料第四项

已选择的cell编号: 0
cell值: 资料第一项

Swift 程式范例

//-----------start-----------
//
//  ViewController.swift
//
//

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    // Cell 资料使用阵列储存,你也可以使用其他方式提供
    let dyItems: NSArray = ["资料第一项", "资料第二项", "资料第三项", "资料第四项"]

    override func viewDidLoad() {
        super.viewDidLoad()

        // 取得Status Bard高度.
        let statusbarHeight: CGFloat = UIApplication.sharedApplication().statusBarFrame.size.height

        // 取得View的宽、高(给table view设定使用)
        let displayWidth: CGFloat = self.view.frame.width
        let displayHeight: CGFloat = self.view.frame.height

        // TableView初始化设定
        // y = statusbarHeight ,避免tableview盖到statusbar (iOS7之后的关系,view的位置可以对到全萤幕)
        // width = displayWidth
        // height = displayHeight
        //
        let dyTableView: UITableView = UITableView(frame: CGRect(x: 0, y: statusbarHeight, width: displayWidth, height: displayHeight - statusbarHeight))

        // 初始化cell的样式及名称,告诉UITableView每个cell显示的样式,这里指定只显示预设(一行文字)
        dyTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "DyDataCell")

        // DataSource 设定self,必需写好对应的function才能取得正确资料
        dyTableView.dataSource = self

        // Delegate 设定self,必需写好对应的delegate function才能取得正确资讯
        dyTableView.delegate = self

        // 将建立好的tableview加入原先的View
        self.view.addSubview(dyTableView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    //点选cell后会呼叫此function告知哪个cell已经被选择(0开始)
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        println("已选择的cell编号: \(indexPath.row)")
        println("cell值: \(dyItems[indexPath.row])")
        print("\r\n")
    }

    //返回总共有多少cell笔数
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dyItems.count
    }

    //根据cellForRowAtIndexPath来取得目前TableView需要哪个cell的资料
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        // 取得tableView目前使用的cell
        let cell = tableView.dequeueReusableCellWithIdentifier("DyDataCell", forIndexPath: indexPath) as UITableViewCell

        // 将指定资料显示于tableview提供的text
        cell.textLabel?.text = "\(dyItems[indexPath.row])"

        return cell
    }

}

//------------end------------

Objective-C 程式范例

标头档

//-----------start-----------
//
//  ViewController.h
//
//
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>


@end

//------------end------------

程式档

//-----------start-----------
//
//  ViewController.m
//
//
//

#import "ViewController.h"
#import <UIKit/UIKit.h>


@interface ViewController ()

@end

@implementation ViewController
{
    NSArray *dyItems;
    UITableView *dyTableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // Cell 资料使用阵列储存,你也可以使用其他方式提供
    dyItems = @[@"资料第一项", @"资料第二项", @"资料第三项", @"资料第四项"];

    // 取得Status Bard高度.
    CGFloat statusbarHeight = UIApplication.sharedApplication.statusBarFrame.size.height;

    // 取得View的宽、高(给table view设定使用)
    CGFloat displayWidth = self.view.frame.size.width;
    CGFloat displayHeight = self.view.frame.size.height;

    // TableView初始化设定
    // y = statusbarHeight ,避免tableview盖到statusbar (iOS7之后的关系,view的位置可以对到全萤幕)
    // width = displayWidth
    // height = displayHeight
    //
    dyTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, statusbarHeight, displayWidth, displayHeight - statusbarHeight)];

    // 初始化cell的样式及名称,告诉UITableView每个cell显示的样式,这里指定只显示预设(一行文字)
    UITableViewCell *tableViewCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DyDataCell"];

    // 设定Cell名称、样式
    [dyTableView registerClass:tableViewCell.class forCellReuseIdentifier:tableViewCell.reuseIdentifier];

    // DataSource 设定self,必需写好对应的function才能取得正确资料
    dyTableView.dataSource = self;

    // Delegate 设定self,必需写好对应的delegate function才能取得正确资讯
    dyTableView.delegate = self;

    // 将建立好的tableview加入原先的View
    [self.view addSubview:dyTableView];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//点选cell后会呼叫此function告知哪个cell已经被选择(0开始)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"已选择的cell编号:%ld",indexPath.row);
    NSLog(@"cell值: %@",[dyItems objectAtIndex:indexPath.row]);
    NSLog(@"\r\n");
}

//返回总共有多少cell笔数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return dyItems.count;
}

//根据cellForRowAtIndexPath来取得目前TableView需要哪个cell的资料
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // 取得tableView目前使用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DyDataCell" forIndexPath: indexPath];

    // 将指定资料显示于tableview提供的text
    cell.textLabel.text = dyItems[indexPath.row];

    return cell;
}

@end
//------------end------------