在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];
}
运行一下就行了。