【问题标题】:Can't access the IBOutlets of my custom cell on cellForRowAtIndexPath无法访问 cellForRowAtIndexPath 上我的自定义单元格的 IBOutlets
【发布时间】:2011-11-01 12:22:22
【问题描述】:

我用 XIB 制作了一个自定义单元格: .h

#import <UIKit/UIKit.h>

@interface TWCustomCell : UITableViewCell {
    IBOutlet UILabel *nick;
    IBOutlet UITextView *tweetText;
}

@end

.m

#import "TWCustomCell.h"

@implementation TWCustomCell

- (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
}

@end

我以这种方式将它们加载到cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
    TWCustomCell *cell = (TWCustomCell*)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"TWCustomCell" owner:nil options:nil];

        for (id currentObject in topLevelObject) {
            if([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (TWCustomCell*) currentObject;
                break;
            }
        }
    }
    // Configure the cell...
    cell.tweetText.text = [tweets objectAtIndex:indexPath.row];
    return cell;
}

开启cell.tweetText.text = [tweets objectAtIndex:indexPath.row];cell 之后的点上,Xcode 告诉我_“在 'TWCustomCell *' 类型的对象上找不到属性 'tweetText';你的意思是访问 ivar 'tweetText' 吗?”并告诉我用 cell-&gt;tweetText.text。但出现错误:“语义问题:实例变量 'tweetText' 受保护”。我该怎么办?

【问题讨论】:

    标签: ios cocoa-touch uitableview uikit


    【解决方案1】:

    您没有声明允许使用点语法访问类外部的 IBOutlets 的属性。

    我会这样做:

    在您的 .h 文件中:

    @property (nonatomic, readonly) UILabel *nick;
    @property (nonatomic, readonly) UITextView *tweetText;
    

    在.m:

    @synthesize nick, tweetText;
    

    或者您可以删除 ivar IBOutlets 并将属性声明为 retain 和 IBOutlets,如下所示:

    @property (nonatomic, retain) IBOutlet UILabel *nick;
    @property (nonatomic, retain) IBOutlet UITextView *tweetText;
    

    【讨论】:

    • 在执行此操作时再次收到错误消息:“语义问题:实例变量 'tweetText' 受保护”,但在使用 @public 时没有
    • @zad0xsis 你应该使用“.”而不是“->”,它会起作用。使用属性是约定,访问公共 ivars(带有“->”)是违反约定的
    【解决方案2】:

    问题出在我的自定义单元格 ivars 上:

    #import <UIKit/UIKit.h>
    
    @interface TWCustomCell : UITableViewCell {
        //added here @public and you can access them now
        @public
        IBOutlet UILabel *nick;
        IBOutlet UITextView *tweetText;
    }
    
    @end
    

    【讨论】:

    • 你可以将它们声明为@property
    • 与另一个对象的实例变量混淆是丑陋而笨拙的。使用@public 声明它们是一种创可贴,可以启用默认禁用的某些功能,这是有充分理由的。您确实应该声明 (@property) 并使用 (.,而不是 -&gt; — 即您开始使用的表达式) 属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多