• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 10:41:16 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有1个部分和6行的tableview。我想在第一行中应用UITableViewCell的自定义单元格样式子类,而其余各行具有默认的UITableViewCell。当我运行该应用程序时,它不会以自定义单元格样式显示第一行的内容。我尝试使用return cell2而不是cell来引用第一行中的自定义样式,但是它仍然无法正常工作。

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)initWithStyleUITableViewCellStyle)style reuseIdentifierNSString *)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 *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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;

}



Best Answer-推荐答案


问题是您始终都返回cell。相反,如果该行为0,则返回cell2(imageCellCell实例)。

对您的代码进行了修改(请注意,该代码尚未经过完全测试):

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap