OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在TableView的行中应用不同的单元格样式 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:41
标题: ios - 如何在TableView的行中应用不同的单元格样式

我有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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4