首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CustomCell中的标签为零

CustomCell中的标签为零
EN

Stack Overflow用户
提问于 2013-09-06 23:32:54
回答 4查看 4.9K关注 0票数 2

我创建了一个自定义UITableViewCell,其中包含一个UILabel。

在cellForRowAtIndexPath方法中,我初始化自定义单元格并给UILabel一个值。

虽然加载了自定义单元格(单元格的高度高于默认单元格),但我似乎不能给UILabel一个值。

SBDownloadCell.h

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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

代码语言:javascript
复制
- (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。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-09-07 02:24:38

我看到您的属性被标记为IBOutlet,这意味着您有一个带有表视图单元格的接口生成器文件( xib或情节提要)。确保将接口生成器中的正确单元标识符提供给表视图中的原型单元格。您不应该调用initWithStyle:。如果dequeueReusableCellWithIdentifier:在使用UITableViewController和情节提要时返回零,这意味着设置不正确,因为dequeueReusableCellWithIdentifier:应该总是返回一个单元格(如果有必要,它会创建新的单元格)。

为了进一步说明,在使用xibs或故事板时,将永远不会调用表视图单元格的initWithStyle:。加载nib时,正确的init方法是initWithCoder:

问题在static NSString *simpleTableIdentifier = @"SBDownload";中。在故事板中,您已经将标识符设置为SBDownloadCell

票数 4
EN

Stack Overflow用户

发布于 2013-09-06 23:50:34

您需要分配UILabelUIProgressView。现在您为它们设置了属性,但是在-initWithStyle方法中,您需要调用如下

代码语言:javascript
复制
self.title = [[UILabel alloc] initWithFrame:etc...];

如果您对SBDownloadCell上的每个属性执行此操作,则应该正确分配标签。

票数 0
EN

Stack Overflow用户

发布于 2014-05-26 07:42:48

如果您在Storyboard中自定义原型单元格,则需要在故事板中使用segue表示TableViewController,或者如果需要通过编程呈现TableViewController,则需要使用instantiateViewControllerWithIdentifier:方法,

代码语言:javascript
复制
- (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中获得一个零标签。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18668074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档