自定义UItableviewCell内容

本文介绍如何创建自定义的UITableViewCell,包括定义自定义UITableViewCell类、初始化控件及设置样式,并展示了如何在UITableView中使用这些自定义cell。

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

首先创建一个继承于 UITableViewCell的cell <wbr></wbr>本人例子代码如下
自定义cell里面有三个 UILabel和一个 UIImageView
ListringCell.h文件

#import <UIKit/UIKit.h>


@interface ListringCell : UITableViewCell

{

<wbr><wbr><span style="color: #3495af">UILabel</span> * _name;</wbr></wbr>

<wbr><wbr><span style="color: #3495af">UILabel</span> * _time;</wbr></wbr>

<wbr><wbr><span style="color: #3495af">UILabel</span> * _title;</wbr></wbr>

<wbr><wbr><span style="color: #3495af">UIImageView</span> * _touxiang;</wbr></wbr>

}

@property(nonatomic,retain)UILabel * _name;

@property(nonatomic,retain)UILabel * _time;

@property(nonatomic,retain)UILabel * _title;

@property(nonatomic,retain)UIImageView * _touxiang;


@end


ListringCell.m文件

#import "ListringCell.h"


@implementation ListringCell

@synthesize _name,_time,_touxiang,_title;

-(void)dealloc{

<wbr><wbr><span style="color: #0433ff">self</span>.<span style="color: #3495af">_name</span>=<span style="color: #0433ff">nil</span>;</wbr></wbr>

<wbr><wbr><span style="color: #0433ff">self</span>.<span style="color: #3495af">_time</span>=<span style="color: #0433ff">nil</span>;</wbr></wbr>

<wbr><wbr><span style="color: #0433ff">self</span>.<span style="color: #3495af">_title</span>=<span style="color: #0433ff">nil</span>;</wbr></wbr>

<wbr><wbr></wbr></wbr> self._touxiang=nil;

<wbr><wbr> [<span style="color: #0433ff">super</span> <span style="color: #3495af">dealloc</span>];</wbr></wbr>

}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

<wbr><wbr><span style="color: #0433ff">self</span> = [<span style="color: #0433ff">super</span> <span style="color: #3495af">initWithStyle</span>:style <span style="color: #3495af">reuseIdentifier</span>:reuseIdentifier];</wbr></wbr>

<wbr><wbr><span style="color: #0433ff">if</span> (<span style="color: #0433ff">self</span>) {</wbr></wbr>

<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> // Initialization code

//创建三个lable和一个imageview 并添加到cell

<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> UIColor * col=[UIColor scrollViewTexturedBackgr<wbr>oundColor<span style="color: #000000">];</span></wbr>

<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> _name=[[UILabel alloc]initWithFrame:CGRectMake(0, 50, 50, 20)];

<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> _time=[[UILabel alloc]initWithFrame:CGRectMake(50, 50, 270, 20)];

<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> _title=[[UILabel alloc]initWithFrame:CGRectMake(50, 0, 270, 50)];

<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> _touxiang=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];

<wbr><wbr><wbr><wbr><span style="color: #3495af">_touxiang</span>.<span style="color: #3495af">frame</span>=<span style="color: #3495af">CGRectMake</span>(0, 0, 50, 50);</wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> _time.backgroundColor=col;

<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> _name.backgroundColor=col;

<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> _title.backgroundColor=col;

<wbr><wbr><wbr><wbr><wbr><span style="color: rgb(0, 143, 0); font-family: Menlo; line-height: normal;">//name字体自适应大小 <wbr>以及居中显示</wbr></span></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><font color="#3495AF" face="Menlo"><span style="line-height: normal;">_name</span></font><span style="color: rgb(0, 0, 0); font-family: Menlo; line-height: normal;">.</span><font color="#3495AF" face="Menlo"><span style="line-height: normal;">adjustsFontSizeToFitWidt<wbr>h</wbr></span></font> <span style="color: rgb(0, 0, 0); font-family: Menlo; line-height: normal;">=</span> <span style="color: rgb(4, 51, 255); font-family: Menlo; line-height: normal;">YES</span><span style="color: rgb(0, 0, 0); font-family: Menlo; line-height: normal;">;</span></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> _name.textAlignment = NSTextAlignmentCenter;

<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> _title.textAlignment = NSTextAlignmentCenter;

<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> _time.textAlignment = NSTextAlignmentCenter;

<wbr><wbr><wbr><wbr> [</wbr></wbr></wbr></wbr>self addSubview:_name];

<wbr><wbr><wbr><wbr> [</wbr></wbr></wbr></wbr>self addSubview:_time];

<wbr><wbr><wbr><wbr> [</wbr></wbr></wbr></wbr>self addSubview:_touxiang];

<wbr><wbr><wbr><wbr> [</wbr></wbr></wbr></wbr>self addSubview:_title];

<wbr><wbr><wbr><wbr> [</wbr></wbr></wbr></wbr>_title release];

<wbr><wbr><wbr><wbr> [</wbr></wbr></wbr></wbr>_touxiang release];

<wbr><wbr><wbr><wbr> [<span style="color: #3495af">_name</span> <span style="color: #3495af">release</span>];</wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr> [<span style="color: #3495af">_time</span> <span style="color: #3495af">release</span>];</wbr></wbr></wbr></wbr>

<wbr><wbr> }</wbr></wbr>

<wbr><wbr></wbr></wbr> return self;

}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated

{

<wbr><wbr> [<span style="color: #0433ff">super</span> <span style="color: #3495af">setSelected</span>:selected <span style="color: #3495af">animated</span>:animated];</wbr></wbr>


<wbr><wbr></wbr></wbr> // Configure the view for the selected state

}


定义好自己的cell后就需要在你的UItableviewController里面的方法 - ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath 使用自定义cell <wbr>代码如下:</wbr>

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

{

<wbr><wbr><span style="color: #0433ff">static</span> <span style="color: #3495af">NSString</span> *CellIdentifier = <span style="color: #b4261a">@"Cell"</span>;</wbr></wbr>

//用自己的自定义cell类创建cell<wbr></wbr>

<wbr><wbr></wbr></wbr> ListringCell *cell = [tableView dequeueReusableCellWithI<wbr>dentifier<span style="color: #000000">:CellIdentifier];</span></wbr>

<wbr><wbr><span style="color: #0433ff">if</span> (cell==<span style="color: #0433ff">nil</span>) {</wbr></wbr>

<wbr><wbr><wbr><wbr> cell =[[[</wbr></wbr></wbr></wbr>ListringCell alloc] initWithStyle:UITableViewCellStyleDefa<wbr>ult reuseIdentifier<span style="color: #000000">:CellIdentifier]</span>autorelease<span style="color: #000000">];</span></wbr>

<wbr><wbr> }</wbr></wbr>

<wbr><wbr></wbr></wbr> // Configure the cell...

//这里让自定义cell的lable和imageview放入自己需要放的东西

<wbr><wbr> [cell</wbr></wbr> _name ].text=(NSString *)[[tableData objectAtIndex:[indexPath row]]objectForKey:@"nickname"];

<wbr><wbr> [cell</wbr></wbr> _time ].text=(NSString *)[[tableData objectAtIndex:[indexPath row]]objectForKey:@"time"];

<wbr><wbr> [cell</wbr></wbr> _title ].text=(NSString *)[[tableData objectAtIndex:[indexPath row]]objectForKey:@"title"];

<wbr><wbr> cell.</wbr></wbr>_touxiang.image=[UIImage imageNamed:[[tableData objectAtIndex:[indexPath row]] objectForKey:@"touxiang"]];

<wbr><wbr><span style="color: #0433ff">return</span> cell;</wbr></wbr>

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值