2

我创建了一个带有 UILabel 的自定义 UITableViewCell 。

在 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;
}

故事板

在此处输入图像描述

我刚刚打破cell.title.text = @"Test";,这仍然是我所看到的:

在此处输入图像描述

会是什么呢?

注意:我在 iOS7 中使用 Xcode DP-5

4

4 回答 4

4

我看到您的属性标有IBOutlet,这意味着您有一个带有表格视图单元格的 Interface Builder 文件(xib 或故事板)。确保在 Interface Builder 中为表格视图中的原型单元格提供正确的单元格标识符。你不应该打电话initWithStyle:。如果dequeueReusableCellWithIdentifier:在使用 aUITableViewController和情节提要时返回 nil,这意味着设置不正确,因为dequeueReusableCellWithIdentifier:应该始终返回一个单元格(如果必须,它会创建新单元格)。

更详细地说,当使用 xibs 或故事板时,initWithStyle:永远不会调用表格视图单元格。加载 nib 时,正确的 init 方法是initWithCoder:.

问题出在static NSString *simpleTableIdentifier = @"SBDownload";. 在情节提要中,您已将标识符设置为SBDownloadCell.

于 2013-09-07T02:24:38.657 回答
0

你需要分配你UILabel的 s 和UIProgressView. 现在你为它们设置属性,但是在你的-initWithStyle方法中你需要调用类似的东西

self.title = [[UILabel alloc] initWithFrame:etc...];

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

于 2013-09-06T23:50:34.490 回答
0

如果您在 Storyboard 中自定义原型单元格,则需要在 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];
}

如果您使用 [TableViewContrller new] 启动 TableViewController,这将让您在 tableView:cellForRowAtIndexPath 方法中获得一个 nil 标签。

于 2014-05-26T07:42:48.503 回答
0

我遇到了同样的问题,但我的标识符对于创建自定义单元格并在 IBOutlets 自定义标签中显示数据是正确的,但是在单元格分配标签后,标签值为空。所以为此我需要添加 tableview 方法来显示标签值

- (void)tableView:(UITableView *)tableView willDisplayCell:(DropViewTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  {
    cell.lblHSNCode.text = objClass.HSNCode;
  }
于 2017-08-23T10:12:59.890 回答