UITableView的常用方法与示例

本文详细介绍了如何使用UITableView展示分组数据,包括实现UITableViewDataSource和UITableViewDelegate协议的方法,以及如何配置单元格和响应用户交互。

实例方法


dequeueReusableCellWithIdentifier:
初始化一个指定重用标识符的UITableCell对象

两个协议


UITableViewDataSource

tableView:numberOfRowsInSection: (required)
行数在指定分区中
tableView:cellForRowAtIndexPath: (required)
每个Cell显示的具体内容
numberOfSectionsInTableView:
分区的个数
tableView:titleForHeaderInSection:
分区的标题
tableView:canEditRowAtIndexPath:
能否编辑

UITableViewDelegate

tableView:didSelectRowAtIndexPath:
点击cell时执行的委托方法


简单的Demo


效果图



点击细节按钮将显示一个AlertView。如果想显示细节信息,亦可以用Master-Detail模板进行创建项目,里面提供好了很多现成的方法,更加方便和快捷.

初始准备

实现协议,增加成员
@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
{
    NSArray *_redFlowers;
    NSArray *_blueFlowers;
}

预定义数字
#define kSectionCount 2
#define kRedSection 0
#define kBlueSection 1

初始化成员
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    _redFlowers = @[@"Gerbera", @"Peony", @"Rose", @"poppy"];
    _blueFlowers = @[@"Hyacinth", @"Hydrangea", @"Sea Holly", @"Phlox", @"Iris"];
}


实现委托方法

分区及分区标题,分区中的行数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return kSectionCount;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case kRedSection:
            return [_redFlowers count];
        case kBlueSection:
            return [_blueFlowers count];
        default:
            return 0;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case kRedSection:
            return @"Red";
        case kBlueSection:
            return @"Blue";
        default:
            return @"Unknown";
    }
}


显示的Cell信息

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"flowerCell"];
    
    switch (indexPath.section) {
        case kRedSection:
            cell.textLabel.text = _redFlowers[indexPath.row];
            break;
        case kBlueSection:
            cell.textLabel.text = _blueFlowers[indexPath.row];
            break;
        default:
            cell.textLabel.text = @"Unknown";
    }
    
    UIImage *flowerImage;
    flowerImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@", cell.textLabel.text, @".png"]];
    cell.imageView.image = flowerImage;
    
    return cell;
}

这里重用标识符可以是自定义的Cell,也可以是在xib中设置好的系统带的样式,然后在属性标识板中给它设置标识符。
图片是自己个人加入到项目中的。

然后实现选择时弹出AlertView的方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIAlertView *showSelection;
    NSString *messageString;
    
    switch (indexPath.section) {
        case kRedSection:
            messageString = [NSString stringWithFormat:@"You chose the red flower - %@", _redFlowers[indexPath.row]];
            break;
        case kBlueSection:
            messageString = [NSString stringWithFormat:@"You chose the blue flower - %@", _blueFlowers[indexPath.row]];
            break;
        default:
            messageString = @"Unknown";
            break;
    }
    
    showSelection = [[UIAlertView alloc] initWithTitle:@"Flower Selected"
                                               message:messageString
                                              delegate:nil
                                     cancelButtonTitle:@"OK"
                                     otherButtonTitles: nil];
    [showSelection show];
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值