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
相关文章推荐
- UITableView获取cell的row值(解决自定义cell里的button通过点击无法获得cell的indexpath的问题)
- 解决点击UITableViewCell时和自定义添加的手势冲突的问题
- 自定义UITableViewCell中获取按钮所在Cell的indexPath
- 自定义UITableViewCellAccessoryCheckmark
- cell中button怎么得到对应cell的indexpath 以及关于UITableViewCellContentView的问题
- iOS开发15:自定义UITableViewCell
- [IOS]用自定义的cell来创建UITableView
- UITableView使用自定义cell的例子
- UITableViewCell有关处理touch事件的问题
- UITableViewCellEditingStyle的问题
- UITableView-的cell 自定义 高度,样式
- 自定义UITableViewCell
- 自定义UITableViewCell
- UITableViewCell的重绘机制导致的列状态问题
- UITableViewCell设置 checkmark的问题
- iOS UITableViewCell重用问题
- Iphone自定义UITableViewCell
- UITableView自定义UITableViewCell
- 自定义UITableViewCell
- UITableView cell自定义视图中插入UITableView