在android开发listView中,每一行的列表可以通过相应的xml定义视图。在iphone开发中,tableView也提供了通过nib自定义视图的解决方案。这就使开发者能够完成相当复杂的界面布局。
下面介绍table中添加自定义的table cell。实现的效果如下:
实现过程很简单,首先创建一个table视图,添加table相应的协议。这一步很简单,在这里就不写如何实现的了。不懂的可以看我以前的博客,或者看源代码。
接下来,新建文件 并在 subclass 里 选择 UITableViewCell 这里我命名为 “MyCell”
然后在利用IB创建一个名为mycell的nib,在里面拖入一个UITableViewCell并将其类名改为MyCell。
实现具体的类:
-(NSInteger) tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return 1;
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCellIdentifier";
MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"mycell" owner:self options:nil];
cell = [array objectAtIndex:0];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
}
[[cell lable] setText:@"你好"];
//[[cell imageView] setImage:[UIImage imageNamed:[imageNameArray objectAtIndex:indexPath.row]]];
//[[cell nameLabel] setText:[nameArray objectAtIndex:indexPath.row]];
return cell;
}
- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 90;
}
项目的源代码:http://easymorse.googlecode.com/svn/trunk/TableCellDemo/