您的位置:首页 > 产品设计 > UI/UE

UITableViewCell的自定义问题

2015-03-04 13:47 260 查看

1.首先,是重写dataSource的代理方法来填充表格

#pragma mark - 实现填充数据方法 UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

returncommentArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{

注意:此处涉及到tableViewCell重用问题,这是为了省内存,建议都这样写

//
定义唯一标识
static
NSString *CellIdentifier =@"Cell";

//
通过唯一标识创建cell实例

CollectTableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

//判断为空进行初始化 --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,而不会再次初始化)important
if (!cell) {

cell = [[CollectTableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:CellIdentifier];
}

// NSLog(@"%@",[[commentArr objectAtIndex:indexPath.row]objectForKey:@"title"]);

cell.comm_title = [[commentArrobjectAtIndex:indexPath.row]objectForKey:@"title"];

cell.comm_content = [[commentArrobjectAtIndex:indexPath.row]objectForKey:@"content"];
return cell;
}

2.自定义TableViewCell

效果图:

CollectTableViewCell.h

#import <UIKit/UIKit.h>

@interface CollectTableViewCell :UITableViewCell

@property (nonatomic,strong) NSString* comm_imageUrl;
@property (nonatomic,strong) NSString* comm_title;
@property (nonatomic,strong) NSString* comm_content;
这里要注意,这样写是错误的,这里必须是自定义的控件,而不是OC对象。

@end

正确的写法:

#import <UIKit/UIKit.h>

@interface CollectTableViewCell : UITableViewCell

@property (nonatomic,retain) UILabel *aTitle;

@property (nonatomic,retain) UILabel *aDetail;

@property (nonatomic,retain) UIImageView*
aImage;

@end

CollectTableViewCell.m

#import "CollectTableViewCell.h"

#import "UIImageView+WebCache.h"

#import "UIViewExt.h"

#define kScreenWith [[UIScreen mainScreen]bounds].size.width

#define kScreenHeight [[UIScreen mainScreen]bounds].size.height

@implementation CollectTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString
*)reuseIdentifier{
self = [superinitWithStyle:style
reuseIdentifier:reuseIdentifier];
if (self) {

//自定义Cell
UIView *aView = [[UIViewalloc]initWithFrame:CGRectMake(8,
8,kScreenWith-16, 120)];

aView.layer.borderColor = [[UIColorgrayColor]
CGColor];
aView.layer.borderWidth = 1;

[self.contentViewaddSubview:aView];

self.aImage = [[UIImageViewalloc]initWithFrame:CGRectMake(8,
8, 80, 80)];

[self.aImagesetImageWithURL:[NSURLURLWithString:@"http://img0.bdstatic.com/img/image/shouye/chongwu0302.jpg"]];
[aViewaddSubview:self.aImage];

self.aTitle = [[UILabelalloc]initWithFrame:CGRectMake(100,
8,kScreenWith-100, 22)];

self.aTitle.font = [UIFontsystemFontOfSize:15];
[aViewaddSubview:self.aTitle];

self.aDetail = [[UILabelalloc]initWithFrame:CGRectMake(100,
30,kScreenWith-100-8*3, 56)];

self.aDetail.numberOfLines = 3;

self.aDetail.font = [UIFontsystemFontOfSize:15];

self.aDetail.textColor = [UIColorblackColor];
[aViewaddSubview:self.aDetail];

UIView *afoot = [[UIViewalloc]initWithFrame:CGRectMake(0,self.aImage.height+16,kScreenWith-16,
120-96)];

afoot.backgroundColor = [UIColorgrayColor];
[aViewaddSubview:afoot];

UIButton *adelete = [[UIButtonalloc]initWithFrame:CGRectMake(kScreenWith-16-45,
0, 45, afoot.height)];

adelete.titleLabel.textColor = [UIColorredColor];

[adelete setTitle:@"删除"forState:UIControlStateNormal];

adelete.titleLabel.font = [UIFontsystemFontOfSize:13];

adelete.backgroundColor = [UIColorgrayColor];
[afootaddSubview:adelete];

}

return
self;
}

- (void)awakeFromNib {

// Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[supersetSelected:selected
animated:animated];

// Configure the view for the selected state
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: