我创建了一个自定义 UITableViewCell,其中包含 UILabel。
在 cellForRowAtIndexPath 方法中,我初始化自定义单元格并为 UILabel 指定一个值。
加载自定义单元格时(单元格的高度高于默认单元格),我似乎无法为 UILabel 指定值。
SBDownloadCell.h
#import <UIKit/UIKit.h>
@interface SBDownloadCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UIProgressView *progressbar;
@property (weak, nonatomic) IBOutlet UILabel *details;
- (IBAction)pause:(id)sender;
@end
SBDownloadCell.m
#import "SBDownloadCell.h"
@implementation SBDownloadCell
- (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
}
- (IBAction)pause:(id)sender {
}
@end
SBViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SBDownload";
SBDownloadCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[SBDownloadCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
SBDownload *dwnld = [[SBDownload alloc] init];
dwnld = [SBdownloads objectAtIndex:indexPath.row];
//cell.title.text = [dwnld name];
cell.title.text = @"Test";
cell.progressbar.progress = [dwnld percentDone];
cell.details.text = [NSString stringWithFormat:@"%f MB of %f MB completed, %@", [dwnld completed], [dwnld length], [dwnld eta]];
return cell;
}
Storyboard
我在 cell.title.text = @"Test";
之后就中断了,但这仍然是我所看到的:
可能是什么?
注意:我使用 Xcode DP-5 和 iOS7
最佳答案
我看到您的属性标有 IBOutlet
,这意味着您有一个带有 TableView 单元格的 Interface Builder 文件(xib 或 Storyboard)。确保在 Interface Builder 中为 TableView 中的原型(prototype)单元提供正确的单元标识符。您不应该调用 initWithStyle:
。如果使用 UITableViewController
和 Storyboard 时 dequeueReusableCellWithIdentifier:
返回 nil,则意味着设置不正确,因为 dequeueReusableCellWithIdentifier:
应该始终返回一个单元格(它会创建如果有必要的话就换新的)。
进一步详细说明,当使用 xib 或 Storyboard 时, TableView 单元格的 initWithStyle:
将永远不会被调用。加载 nib 时,正确的 init 方法是 initWithCoder:
。
问题出在static NSString *simpleTableIdentifier = @"SBDownload";
。在 Storyboard中,您已将标识符设置为 SBDownloadCell
。
关于iphone - CustomCell 中的标签保持为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18668074/