gpt4 book ai didi

ios - 如何在 TableView 的行中应用不同的单元格样式

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:04:50 25 4
gpt4 key购买 nike

我有一个包含 1 个部分和 6 行的表格 View 。我想在第一行应用 UITableViewCell 的自定义单元格样式子类,而其余行具有默认的 UITableViewCell。当我运行该应用程序时,它不会显示具有自定义单元格样式的第一行的内容。我尝试使用 return 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;

}

最佳答案

问题是您始终返回 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;
}

还有一个similar question here .

关于ios - 如何在 TableView 的行中应用不同的单元格样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24635813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com