定制UITableViewCell
2013-11-18 17:03
239 查看
第一、定制UITableViewCell的几种方式:
1.通过UITableViewCell固定格式设置,其属性是ImageView、textLabel、detailLabel,通过改变这些控件来改变位置,这种方法不够灵活
2.通过UITableViewCell的contentView属性添加子视图
3.使用UITableViewCell的xib自定义子视图,布局方便
4.子类化UITableViewCell,更加面向对象
第二、sample code:
通过UITableViewCell的contentView属性添加子视图:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *label=[[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)] autorelease];
label.tag=101;
[cell.contentView addSubview:label];
}
UILabel *label=(UILabel*)[cell.contentView viewWithTag:101];
NSLog(@"the clicked row is %d",indexPath.row);
label.text=[listArray objectAtIndex:indexPath.row];
return cell;
注意上述代码中先通过contentView addSubview加载子视图,为了更好地利用cell我们是在cell==nil空时,加载label,这样不用每个cell加label
使用UITableViewCell的xib自定义子视图:
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil)
{
NSArray *nibs=[[NSBundle mainBundle] loadNibNamed:@"mycell" owner:self options:nil];
cell=[nibs objectAtIndex:0];
}
UILabel *label=(UILabel*)[cell.contentView viewWithTag:101];
label.text=[listArray objectAtIndex:indexPath.row];
UIImageView *imageView=(UIImageView*)[cell.contentView viewWithTag:201];
imageView.backgroundColor=[UIColor redColor];
return cell;
第三、子类tableViewCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self)
{
self.nameLabel=[[[UILabel alloc] initWithFrame:CGRectMake(0, 5, 70, 15)] autorelease];
//nameLabel.text=self.name;
[self.contentView addSubview:self.nameLabel];
self.colorLable=[[[UILabel alloc] initWithFrame:CGRectMake(0, 26, 70, 15)] autorelease];
// colorLabel.text=self.color;
[self.contentView addSubview:self.colorLable];
}
return self;
}
创建cell时采用如下函数进行实现:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier=@"SimpleTableIdentifier";
AndyTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
if(cell==nil)
{
cell=[[AndyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier];
}
cell.name=[[self.listdata objectAtIndex:indexPath.row] objectForKey:@"Name"];
cell.color=[[self.listdata objectAtIndex:indexPath.row] objectForKey:@"Color"];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
return cell;
}
1.通过UITableViewCell固定格式设置,其属性是ImageView、textLabel、detailLabel,通过改变这些控件来改变位置,这种方法不够灵活
2.通过UITableViewCell的contentView属性添加子视图
3.使用UITableViewCell的xib自定义子视图,布局方便
4.子类化UITableViewCell,更加面向对象
第二、sample code:
通过UITableViewCell的contentView属性添加子视图:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *label=[[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)] autorelease];
label.tag=101;
[cell.contentView addSubview:label];
}
UILabel *label=(UILabel*)[cell.contentView viewWithTag:101];
NSLog(@"the clicked row is %d",indexPath.row);
label.text=[listArray objectAtIndex:indexPath.row];
return cell;
注意上述代码中先通过contentView addSubview加载子视图,为了更好地利用cell我们是在cell==nil空时,加载label,这样不用每个cell加label
使用UITableViewCell的xib自定义子视图:
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil)
{
NSArray *nibs=[[NSBundle mainBundle] loadNibNamed:@"mycell" owner:self options:nil];
cell=[nibs objectAtIndex:0];
}
UILabel *label=(UILabel*)[cell.contentView viewWithTag:101];
label.text=[listArray objectAtIndex:indexPath.row];
UIImageView *imageView=(UIImageView*)[cell.contentView viewWithTag:201];
imageView.backgroundColor=[UIColor redColor];
return cell;
第三、子类tableViewCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self)
{
self.nameLabel=[[[UILabel alloc] initWithFrame:CGRectMake(0, 5, 70, 15)] autorelease];
//nameLabel.text=self.name;
[self.contentView addSubview:self.nameLabel];
self.colorLable=[[[UILabel alloc] initWithFrame:CGRectMake(0, 26, 70, 15)] autorelease];
// colorLabel.text=self.color;
[self.contentView addSubview:self.colorLable];
}
return self;
}
创建cell时采用如下函数进行实现:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier=@"SimpleTableIdentifier";
AndyTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
if(cell==nil)
{
cell=[[AndyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier];
}
cell.name=[[self.listdata objectAtIndex:indexPath.row] objectForKey:@"Name"];
cell.color=[[self.listdata objectAtIndex:indexPath.row] objectForKey:@"Color"];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
return cell;
}
相关文章推荐
- storyboard中用xib文件创建定制的UITableViewCell对象 .
- 定制UITableviewCell实现类似微博的分组效果
- 给定制的UITableViewCell里的控件点击设置代理
- 使用工厂模式定制UITableViewCell
- iOS 在自定制UITableViewCell 在cell的中实现点击跳页面
- 定制可隐藏seperator的UITableViewCell
- 定制UITableviewCell实现类似微博的分组效果
- xcode4.3.1定制UITableviewCell实现类似微博的分组效果
- 定制UITableViewCell
- storyboard中用xib文件创建定制的UITableViewCell对象
- UITableViewCell定制
- xcode4.3.1定制UITableviewCell实现类似微博的分组效果
- UITableViewCell定制总结
- 定制 UITableViewCell 表视图单元格
- UITableViewCell定制
- [iOS开发必备技巧之]使用IB定制UITableViewCell
- 使用XIB文件来定制你的UITableViewCell
- Android源码中修改默认语言 及 定制多语言
- Linux定制history命令的输出格式
- 给UITableViewCell动态调整高度