我有一个包含1个部分和6个行的tableview。我想将第一行应用于UITableViewCell的自定义单元格样式子类,而其余行具有默认的UITableViewCell。当我运行应用程序时,它不会显示具有自定义单元格样式的第一行的内容。我尝试使用返回cell2,它引用第一行中的自定义样式而不是单元格,但它仍然无效。
imageCellCell.h
#import <UIKit/UIKit.h>
@interface imageCellCell : UITableViewCell
@property (nonatomic, strong) UIView *view;
@property (nonatomic, strong) UILabel *label1;
@property (nonatomic, strong) UILabel *label2;
@property (nonatomic, strong) UIImageView *prodimage;
@end
imageCellCell.m
#import "imageCellCell.h"
@implementation imageCellCell
@synthesize view;
@synthesize label1;
@synthesize label2;
@synthesize prodimage;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
view = [[UIView alloc] initWithFrame:self.frame];
[self addSubview:view];
// initiate image
prodimage = [[UIImageView alloc]initWithFrame:CGRectMake(30,2, 180, 180)];
// initiate label1 with position (10,10,150,20)
label1 = [[UILabel alloc] initWithFrame:CGRectMake(10,10,150,20)];
// initiate label2 with position (170,10,150,20)
label2 = [[UILabel alloc] initWithFrame:CGRectMake(170,10,150,20)];
[view addSubview:prodimage];
[view addSubview:label1];
[view addSubview:label2];
}
return self;
}
@end
tableviewcontroller.m
#import "ScannedProductControllerViewController.h"
#import "imageCellCell.h"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier;
NSString *CellIdentifierimg;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
imageCellCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifierimg];
if (cell2 == nil) {
cell2 = [[imageCellCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierimg];
}
if (indexPath.row == 1) {
} else if (indexPath.row == 2) {
}
switch ([indexPath row])
{
case 0:
{
[cell2.prodimage setImage: [UIImage imageNamed:@"test1.jpg"]];
[cell2.label1 setText:@"text"];
[cell2.label2 setText:@"more text"];
cell2.accessoryType = UITableViewCellAccessoryNone;
break;
}
case 1:
{
NSString *labelText = [self.data objectAtIndex:indexPath.row];
cell.textLabel.text = labelText;
break;
}
case 2:
{
cell.textLabel.text = @"Manufacturer";
break;
}
case 3:
{
cell.textLabel.text = @"Overall Score";
break;
}
case 4:
{
cell.textLabel.text = @"Description";
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
break;
}
case 5:
{
cell.textLabel.text = @"Video";
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
break;
}
}
return cell;
}
答案 0 :(得分:2)
问题是您始终返回cell
。相反,如果行为0,则返回cell2
(imageCellCell
实例)。
修改代码(注意代码未经过全面测试):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier;
NSString *CellIdentifierimg;
UITableViewCell *cell;
if (cell == nil) {
if (indexPath.row == 1) {
cell = [[imageCellCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierimg];
} else if (indexPath.row == 2) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
}
switch ([indexPath row])
{
case 0:
{
imageCellCell *firstRowCell = (imageCellCell *)cell;
[firstRowCell.prodimage setImage: [UIImage imageNamed:@"test1.jpg"]];
[firstRowCell.label1 setText:@"text"];
[firstRowCell.label2 setText:@"more text"];
firstRowCell.accessoryType = UITableViewCellAccessoryNone;
break;
}
case 1:
{
NSString *labelText = [self.data objectAtIndex:indexPath.row];
cell.textLabel.text = labelText;
break;
}
case 2:
{
cell.textLabel.text = @"Manufacturer";
break;
}
case 3:
{
cell.textLabel.text = @"Overall Score";
break;
}
case 4:
{
cell.textLabel.text = @"Description";
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
break;
}
case 5:
{
cell.textLabel.text = @"Video";
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
break;
}
}
return cell;
}