我想在UITableView
中设置自定义单元格,我通过故事板创建我的UIView
并将其链接到代码
我不知道为什么我的照片没有出现
请检查我的代码吗?
自定义代码.h
: UITableViewCell
@property (strong, nonatomic) IBOutlet UIImageView *weekImg;
@end
自定义代码.m
@synthesize weekImg;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
weekImg=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"red.png"]];
}
return self;
}
我的方法:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
WeekTableViewCell *cell = (WeekTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"WeekTableViewCell"];
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
[[cell textLabel] setText:contentForThisRow];
return cell;
}
答案 0 :(得分:0)
我认为这是因为您的代码只是寻找优先创建的单元格。在开始时,不会创建任何单元格,也无法以任何方式检索。
WeekTableViewCell *cell = (WeekTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"WeekTableViewCell"];
if (cell == nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"WeekTableViewCell" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[WeekTableViewCell class]])
{
cell = (WeekTableViewCell *)currentObject;
break;
}
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
[[cell textLabel] setText:contentForThisRow];
return cell;
如果没有要重新使用的单元格
,这样就会创建一个新单元格答案 1 :(得分:0)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.imageView.layer.cornerRadius=10.0f;
[cell.imageView.layer setMasksToBounds:YES];
cell.textLabel.text=[NSMUtableArray objectAtIndex:indexPath.row];
switch (indexPath.row)
{
case 0:
cell.imageView.image=[UIImage imageNamed:@"img1.png"];
break;
case 1:
cell.imageView.image=[UIImage imageNamed:@"img2.png"];
break;
case 2:
cell.imageView.image=[UIImage imageNamed:@"img3.png"];
break;
case 3:
cell.imageView.image=[UIImage imageNamed:@"img4.png"];
break;
default:
break;
}
}
cell.imageView.layer.cornerRadius=10.0f;
[cell.imageView.layer setMasksToBounds:YES];, it is used by QuartzCore.
像这样的单元格的输出,我们可以调整图像大小。