我用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->tweetText.text。但是出现错误:“语义问题:实例变量'tweetText'受保护”。我需要做什么?

最佳答案

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

这是我的方法:

在您的.h文件中:

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

在他们之中:
@synthesize nick, tweetText;

或者,您可以删除ivar IBOutlets并将属性声明为keep和IBOutlets,如下所示:
@property (nonatomic, retain) IBOutlet UILabel *nick;
@property (nonatomic, retain) IBOutlet UITextView *tweetText;

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

10-09 02:33