`
844604778
  • 浏览: 549816 次
文章分类
社区版块
存档分类
最新评论

自定义UItableviewCell内容

 
阅读更多
首先创建一个继承于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>

}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics