gpt4 book ai didi

带有自定义单元格的IOS静态表只绘制一个随机单元格

转载 作者:行者123 更新时间:2023-12-01 17:27:16 25 4
gpt4 key购买 nike

我正在尝试为我的应用程序创建一个“设置” TableView 。我试图模仿它与 Iphone 上的一般设置相同的风格。我通过从 UITableCell 继承创建了自己的自定义单元格类。我给了它适当的 IBOulet,并将它们连接到 Storyboard 中。我还将开关连接到我的 tableViewControler,但由于某种原因,我的代码只返回一个空单元格(它只是一个单元格不是问题 atm,因为这就是我的设置中的全部内容)。我三重检查并确保我在我的代码和 Storyboard 中使用了相同的单元格标识符。任何人都知道为什么我得到一个空白单元格?

这是我的自定义单元格的 .h 文件。

@interface NHPSettingsCell : UITableViewCell
@property (nonatomic,weak) IBOutlet UILabel *settingLabel;
@property (nonatomic,strong) IBOutlet UISwitch *settingSwitch;
@end

我的问题代码在这里,我的自定义单元格的 .h 文件:
#import "NHPSettingsCell.h"

@implementation NHPSettingsCell
@synthesize settingLabel, settingSwitch;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

@end

我在自定义 View Controller 中绘制单元格的方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"SettingsCell";
NHPSettingsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[NHPSettingsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellStyleDefault;
}
cell.settingLabel.text = @"firstSetting";

//check the if user wants promt of disclaimer each time or not.
if([prefs boolForKey:@"firstSetting"] == YES){
cell.settingSwitch.on = YES;
}else{
cell.settingSwitch.on = NO;
}

return cell;
}

现在让我烦恼的是我已经成功地为使用自定义单元格的动态表实现了 cellForRowAtIndexPath 方法。我还使用默认单元格实现了静态表的代码,但是对于带有自定义单元格的静态表,它似乎不起作用。这是关于我如何在动态表上实现自定义单元格的代码(注意我如何不必初始化单元格,但它可以工作)。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"InteractionResultCell";
NHPResultCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


// Configure & Fill the cell
cell.leftLabel.text = [[resultsList objectAtIndex:indexPath.row] substanceName];
cell.rightLabel.text = [[resultsList objectAtIndex:indexPath.row] substanceName2];
NSString *color = [NSString stringWithFormat:@"%@", [[resultsList objectAtIndex:indexPath.row] color]];

//Change a hex value to a readable 0x number to pass ot hte macro so we can go from a hex color to a RGB system.
NSScanner *scanner;
unsigned int tempint=0;
scanner = [NSScanner scannerWithString:color];
[scanner scanHexInt:&tempint];

cell.severityButton.backgroundColor = UIColorFromRGB(tempint);


return cell;
}

最佳答案

两个问题:

  • 如果您使用的是静态单元格,请不要在 View Controller 中实现任何数据源方法(numberOfRows、numberOfSections、cellForRow...),因为这将覆盖您在 Storyboard 中构建的内容。该表具有您在 Storyboard 中提供的部分、行和内容。
  • 从 Storyboard 加载的单元格(动态原型(prototype)或静态单元格)使用 initWithCoder: 初始化,而不是 initWithStyle: . awakeFromNib:是放置设置代码的更好地方。
  • 关于带有自定义单元格的IOS静态表只绘制一个随机单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9883107/

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