我是UITableViewCell的子类,它导致我的应用程序崩溃...我遇到的问题是我的单元格一直在自己绘制,所以我在搜索后找到的答案是子类,但我遇到了一些问题。这是我的代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *requestCell = @"requestCell";
RequestCell *cell;
switch (indexPath.section) {
{case 0:
cell = [tableView dequeueReusableCellWithIdentifier:requestCell forIndexPath:indexPath];
if (cell == nil) {
cell = [[RequestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:requestCell];
}
//APP CRASHES ON THIS LINE
cell.requestTitle.frame = CGRectMake(60, 5, self.view.frame.size.width - 70, 30);
cell.detailTitle.frame = CGRectMake(60, 30, self.view.frame.size.width - 70, 25);
Request *request = [[Request alloc] init];
request = self.isNotFilledList[indexPath.row];
cell.requestTitle.text = request.productName;
cell.detailTitle.text = request.dateRequested;
cell.detailTitle.font = [UIFont systemFontOfSize:11.0];
NSString *imageURL = @"myurl";
imageURL = [imageURL stringByAppendingString:request.productImageName];
cell.requestImageButton.frame = CGRectMake(5, 5, 50, 50);
[cell.requestImageButton sd_setBackgroundImageWithURL:[NSURL URLWithString:imageURL] forState:UIControlStateNormal];
NSLog(@"request name %@", request.productName);
return cell;
break;}
{case 1:
cell = [tableView dequeueReusableCellWithIdentifier:requestCell forIndexPath:indexPath];
if (cell == nil) {
cell = [[RequestCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:requestCell];
}
Request *request = [[Request alloc] init];
request = [self.isFilledList objectAtIndex:indexPath.row];
cell.textLabel.text = request.productName;
return cell;
break;}
default:
break;
}
return cell;
}
我的RequestCell类
@interface RequestCell : UITableViewCell
@property (strong, nonatomic) UILabel *requestTitle;
@property (strong, nonatomic) UILabel *detailTitle;
@property (strong, nonatomic) UIButton *requestImageButton;
和.m文件
#import "RequestCell.h"
@implementation RequestCell
- (void)awakeFromNib {
// Initialization code
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
}
return self;
}
-(UILabel *)requestTitle
{
if(!_requestTitle)
{
_requestTitle = [[UILabel alloc] init];
}
return _requestTitle;
}
-(UILabel *)detailTitle
{
if(!_detailTitle)
{
_detailTitle = [[UILabel alloc] init];
}
return _detailTitle;
}
-(UIButton *)requestImageButton
{
if(!_requestImageButton)
{
_requestImageButton = [[UIButton alloc] init];
}
return _requestImageButton;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
我做错了什么???
原始代码**重新绘制顶部的单元格
switch (indexPath.section) {
{case 0:
cell = [tableView dequeueReusableCellWithIdentifier:requestCell forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:requestCell];
}
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(55, 5, self.view.frame.size.width - 60, 30)];
UILabel *detailTitle = [[UILabel alloc] initWithFrame:CGRectMake(55, 30, self.view.frame.size.width - 60, 25)];
Request *request = [[Request alloc] init];
request = self.isNotFilledList[indexPath.row];
title.text = request.productName;
detailTitle.text = request.dateRequested;
NSString *imageURL = @"myurl";
imageURL = [imageURL stringByAppendingString:request.productImageName];
UIButton *keyImageButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 50, 50)];
//[[keyImageButton imageView] setContentMode: UIViewContentModeScaleAspectFill];
[keyImageButton sd_setBackgroundImageWithURL:[NSURL URLWithString:imageURL] forState:UIControlStateNormal];
[cell addSubview:detailTitle];
[cell addSubview:title];
[cell addSubview:keyImageButton];
NSLog(@"request name %@", request.productName);
return cell;
break;}
答案 0 :(得分:3)
如果您的单元格是在故事板中制作的,那么initWithCoder:
是调用的init方法,而不是initWithStyle:reuseIdentifier:
。因此,覆盖它,并创建标签,并将它们添加到contentView。此外,没有理由在cellForRowAtIndexPath
方法中使用if cell == nil子句,因为您的出队方法可以保证返回一个单元格。
你应该改变的另一件事就是这个,
Request *request = [[Request alloc] init];
request = self.isNotFilledList[indexPath.row];
您在第一行中实例化一个Request对象,但随后通过在下一行中将其重新定义为self.isNotFilledList [indexPath.row]来抛弃它。你应该有,
Request *request = self.isNotFilledList[indexPath.row];
答案 1 :(得分:0)
您不需要手动实例化Cell实例变量。尝试删除额外的功能
-(UILabel *)requestTitle
{
if(!_requestTitle)
{
_requestTitle = [[UILabel alloc] init];
}
return _requestTitle;
}
-(UILabel *)detailTitle
{
if(!_detailTitle)
{
_detailTitle = [[UILabel alloc] init];
}
return _detailTitle;
}
-(UIButton *)requestImageButton
{
if(!_requestImageButton)
{
_requestImageButton = [[UIButton alloc] init];
}
return _requestImageButton;
}
赞成这个
@implementation RequestCell
@synethesize requestTitle;
@synthesize detailTitle;
@synthesize requestImageButton;
编辑:
啊我觉得我现在看到了问题。您正在使用dequeueReusableCellWithIdentifier:forIndexPath:
这是新的iOS 6并且设计为永远不会返回nil单元格(以防止应用程序崩溃和开发人员的便利)。所以你的if语句是针对OLD出列方法的,它只是dequeueReusableCellWithIdentifier
修复应该改变这个:
cell = [tableView dequeueReusableCellWithIdentifier:requestCell forIndexPath:indexPath];
到此:
cell = [tableView dequeueReusableCellWithIdentifier:requestCell];