ios - 无法访问 cellForRowAtIndexPath 上自定义单元格的 IBOutlet

标签 ios cocoa-touch uitableview uikit

我用 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;
}

On cell.tweetText.text = [tweets objectAtIndex:indexPath.row];cell 之后的点上,Xcode 告诉我:“在‘TWCustomCell *’类型的对象上找不到属性‘tweetText’;您是想访问 ivar ‘tweetText’吗?”并告诉我将其替换为 单元格->tweetText.text。但出现错误:“语义问题:实例变量'tweetText' protected ”。我必须做什么?

最佳答案

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

我会这样做:

在您的 .h 文件中:

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

在.m中:

@synthesize nick, tweetText;

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

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

关于ios - 无法访问 cellForRowAtIndexPath 上自定义单元格的 IBOutlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7966358/

相关文章:

ios - 根据用户位置动态绘制RMShape

ios - 在 viewDidAppear 之前以编程方式定位 UIView

objective-c - 在 UIView 之间拖动 UIView

iphone - 错误 "Application tried to push a nil view controller on target <UINavigationController: 0x498efa0>."是什么?

ios - UIImage 中的编码字符串返回 null

ios - 如何在 switch 语句中使用 !=?

ios - 访问 iOS 应用程序的最前端 Controller

iphone - 杀死 View 并返回到 rootViewController

iphone - Xcode 3.2.4 分析器跳过了这个文件?

objective-c - 如何在 UITableViewCell 中添加复选标记