我创建了一个自定义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;
@endSBDownloadCell.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 {
}
@endSBViewController.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;
}故事板

我在cell.title.text = @"Test";之后就崩溃了,这仍然是我所看到的:

会是什么?
注意:我在iOS7中使用Xcode DP-5。
发布于 2013-09-07 02:24:38
我看到您的属性被标记为IBOutlet,这意味着您有一个带有表视图单元格的接口生成器文件( xib或情节提要)。确保将接口生成器中的正确单元标识符提供给表视图中的原型单元格。您不应该调用initWithStyle:。如果dequeueReusableCellWithIdentifier:在使用UITableViewController和情节提要时返回零,这意味着设置不正确,因为dequeueReusableCellWithIdentifier:应该总是返回一个单元格(如果有必要,它会创建新的单元格)。
为了进一步说明,在使用xibs或故事板时,将永远不会调用表视图单元格的initWithStyle:。加载nib时,正确的init方法是initWithCoder:。
问题在static NSString *simpleTableIdentifier = @"SBDownload";中。在故事板中,您已经将标识符设置为SBDownloadCell。
发布于 2013-09-06 23:50:34
您需要分配UILabel和UIProgressView。现在您为它们设置了属性,但是在-initWithStyle方法中,您需要调用如下
self.title = [[UILabel alloc] initWithFrame:etc...];如果您对SBDownloadCell上的每个属性执行此操作,则应该正确分配标签。
发布于 2014-05-26 07:42:48
如果您在Storyboard中自定义原型单元格,则需要在故事板中使用segue表示TableViewController,或者如果需要通过编程呈现TableViewController,则需要使用instantiateViewControllerWithIdentifier:方法,
- (IBAction)showTableView:(id)sender
{
UIStoryboard *storybaord = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil];
LJTableViewController *vc = (LJTableViewController*)[storybaord
instantiateViewControllerWithIdentifier:@"TableViewControllerID"];
[self presentViewController:vc animated:YES completion:nil];
}如果您使用TableViewController new启动TableViewContrller,这将使您在tableView:cellForRowAtIndexPath mehtod中获得一个零标签。
https://stackoverflow.com/questions/18668074
复制相似问题