IOS Xcode5 IB tableView 简单使用

本文介绍了如何在Xcode5中利用Interface Builder(IB)设置tableView,包括将tableView的Delegate和dataSource与file owner关联,实现UITableViewDataSource和UITableViewDelegate协议,以及填充数据并响应选择事件。通过实例展示了从创建tableView到实现基本功能的完整过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Xcode5上使用IB界面做tableView的简单的使用。

1,在IB上加入一个table view。2,右键table view,将这个table view的Delegate和dataSource分别与file ownsr关联起来,要关联两次。3,在这个IB对应的controller的头文件中添加协议<UITableViewDataSource,UITableViewDelegate>,并且定义一个列表作为属性@property(nonatomic,strong)NSArray * list;4,在controller源文件中,先要@synthesize之前定义的list,然后在viewDidLoad中初始化要显示的内容,举个简单例子:NSArray * array = [[NSArray alloc]initWithObjects:@"简体中文",@"English",@"日本語",nil ]; self.list = array;我们遵守的协议中有三个方法是必须实现的,实现可以参考以下:

-(int)tableView:(UITableView*)tableView

numberOfRowsInSection:(NSInteger)section

{

    return [self.list count];

}

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

{

    static NSString * TableIdentifier = @"TableIdentifier";

    UITableViewCell * cell

    = [tableView dequeueReusableCellWithIdentifier:TableIdentifier];

    if(nil == cell)

    {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableIdentifier];

    }

    NSUInteger row = [indexPath row];

    cell.textLabel.text = [self.list objectAtIndex:row];

    return cell;

}

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

{

    NSString *rowString = [self.list objectAtIndex:[indexPath row]];

    UIAlertView * alter = [[UIAlertView alloc] initWithTitle:@"您选择的语言:" message:rowString delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

    [alter show];

}

运行一下就行了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值