gpt4 book ai didi

ios:自定义 UITableViewCell 为空

转载 作者:行者123 更新时间:2023-11-29 03:47:22 26 4
gpt4 key购买 nike

我不明白为什么它无法加载我的内容。这是单元格的 .m 和 .h 文件(还有一个 .xib 文件)

TCMExploreLevelCell.h

#import <Foundation/Foundation.h>

@interface TCMExploreLevelCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *levelImage;
@property (weak, nonatomic) IBOutlet UILabel *levelTitle;

@property (weak, nonatomic) id controller;
@property (weak, nonatomic) UITableView *owningTableView;

@end

TCMExploreLevelCell.m

#import "TCMExploreLevelCell.h"

@implementation TCMExploreLevelCell

- (void)awakeFromNib
{
// Remove automatic constraints
for (UIView *v in [[self contentView] subviews]){
[v setTranslatesAutoresizingMaskIntoConstraints:NO];
}

NSDictionary *names = @{@"image":[self levelImage],
@"title":[self levelTitle]
};

NSString *fmt = @"H:|-10-[image(==42)]-[title]-10-|";

NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:fmt
options:0
metrics:nil
views:names];

[[self contentView] addConstraints:constraints];

NSArray * (^constraintBuilder)(UIView *, float);
constraintBuilder = ^(UIView *view, float height){
return @[
// Constraint 0: Center Y of incoming view to contentView
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:[self contentView]
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0],

// Constraint 1: Pin width of incoming view to constant height
[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:height]
];
};

constraints = constraintBuilder([self levelImage],50);
[[self contentView] addConstraints:constraints];

constraints = constraintBuilder([self levelTitle],21);
[[self contentView] addConstraints:constraints];
}

@end

这是 TableView 中加载行的函数。如果您注意到 NSlog,第一个会返回正确的级别标题。第二个返回单元格的实例,但是在设置 levelTitle 文本后,它在第三个 NSlog 中返回 null

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TCMLevelRemote *l = [[[[TCMExhibitFeedStore sharedStore] allLevels] objectForKey:@"levels"] objectAtIndex:[indexPath row]];

NSLog(@"%@",[l level]);
TCMExploreLevelCell *c = [tableView dequeueReusableCellWithIdentifier:@"TCMExploreLevelCell"];
if(!c){
c = [[TCMExploreLevelCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"TCMExploreLevelCell"];

}
NSLog(@"%@",c);
[c setController:self];
[c setOwningTableView:tableView];

[[c levelTitle] setText:[l level]];

NSLog(@"%@",[c levelTitle]);

if ([l levelid] == 1){
[[c levelImage] setImage:[UIImage imageWithContentsOfFile:@"lowerlevel.png"]];
} else if ([l levelid] == 6) {
[[c levelImage] setImage:[UIImage imageWithContentsOfFile:@"alllevels.png"]];
} else {
[[c levelImage] setImage:[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"level%d.png",[indexPath row]]]];
}

return c;
}

最佳答案

如果您的单元格有 xib 文件,那么您可以在 viewDidLoad 中注册该文件:

[self.tableView registerNib:[UINib nibWithNibName:@"TCMExploreLevelCell" bundle:nil] forCellReuseIdentifier:@"TCMExploreLevelCell"];

然后,在 cellForRowAtIndexPath 中,使用 dequeueReusableCellWithIdentifier:forIndexPath: 而不是 dequeueReusableCellWithIdentifier:。您不需要 if (cell == nil) 子句,该方法保证为您提供一个单元格。这是使用基于 xib 的单元格的推荐方法,而不是在 cellForRowAtIndexPath 中加载 Nib 。

关于ios:自定义 UITableViewCell 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17555528/

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