2011-11-01 42 views
2

我的自定義單元格的IBOutlets我做了一個XIB自定義單元格: .H不能訪問的cellForRowAtIndexPath

#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]; 在d在cell之後,Xcode告訴我「在'TWCustomCell *'類型的對象上找不到'Property'tweetText';你是否想要訪問ivar'tweetText'?「,並告訴我用 cell->tweetText.text替換它,但是出現錯誤:」語義問題:實例變量'tweetText'受保護「。我該怎麼辦?

回答

1

問題是我的自定義單元格的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 
+0

你可以將它們聲明爲@property – Basel

+1

與另一個對象的實例變量混淆是醜陋的,並且非常笨拙。用'@ public'聲明它們是一種創可貼的方法,可以啓用默認情況下禁用的功能。您應該聲明('@ property')並使用('.',而不是' - >' - 即您開始的表達式)屬性。 –

2

你沒有聲明一個屬性,將允許與點語法之類的外部訪問該IBOutlets

下面是我會做:

i n您的.h文件中:

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

@synthesize nick, tweetText; 

或者你可以刪除伊娃IBOutlets和聲明爲保留的屬性和IBOutlets是這樣的:

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

我得到的錯誤:「語義問題:實例變量'tweetText'被保護」,當這樣做,但不是當使用'@ public' – pmerino

+0

@ zad0xsis你應該使用「。」。而不是「 - >」,它會起作用。使用屬性是慣例,訪問公共ivars(以「 - >」)違反公約 –