Не могу получить доступ к IBOutlets моей пользовательской ячейки на cellForRowAtIndexPath - PullRequest
2 голосов
/ 01 ноября 2011

Я сделал пользовательскую ячейку с 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 сообщает мне_ "Свойство 'tweetText' не найдено для объекта типа 'TWCustomCell *'; Вы имели в виду доступ к ivar 'tweetText'?"и говорит мне заменить его на cell->tweetText.text.Но появляется ошибка: «Семантическая проблема: переменная экземпляра tweetText защищена».Что мне делать?

Ответы [ 2 ]

2 голосов
/ 01 ноября 2011

Вы не объявили свойство, которое разрешит доступ к IBOutlets вне класса с синтаксисом точки.

Вот как бы я это сделал:

в вашем .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;
1 голос
/ 01 ноября 2011

Проблема была с моими пользовательскими ячейками:

#import <UIKit/UIKit.h>

@interface TWCustomCell : UITableViewCell {
    //added here @public and you can access them now
    @public
    IBOutlet UILabel *nick;
    IBOutlet UITextView *tweetText;
}

@end
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...