Ios Can';无法访问CellForRowatineXpath上自定义单元格的IBOutlet
我用XIB制作了一个自定义单元格: h 我将它们加载到Ios Can';无法访问CellForRowatineXpath上自定义单元格的IBOutlet,ios,cocoa-touch,uitableview,uikit,Ios,Cocoa Touch,Uitableview,Uikit,我用XIB制作了一个自定义单元格: h 我将它们加载到cellforrowatinexpath:中,方法如下: - (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; TWCustomCell *cell = (TWCustomCell*)
cellforrowatinexpath:
中,方法如下:
- (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]上代码>
在单元格
后面的点上,Xcode告诉我“在类型为“TWCustomCell*”的对象上找不到属性“tweetText”;您是否打算访问ivar“tweetText”?”并告诉我将其替换为
单元格->tweetText.text
。但是出现了错误:“语义问题:实例变量‘tweetText’受保护”。我该怎么办?问题在于我的自定义单元格IVAR:
#import <UIKit/UIKit.h>
@interface TWCustomCell : UITableViewCell {
//added here @public and you can access them now
@public
IBOutlet UILabel *nick;
IBOutlet UITextView *tweetText;
}
@end
#导入
@接口TWCustomCell:UITableViewCell{
//在此处添加@public,您现在可以访问它们
@公开的
IBUILabel*尼克;
IBOutlet UITextView*tweetText;
}
@结束
您没有声明一个属性,该属性将允许使用点语法访问类外的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;
您可以将它们声明为@propertyMessing,而使用另一个对象的实例变量则是丑陋和笨拙的。用<代码> @ Pube < /Cord>声明它们是一个创可贴,可以使默认情况下禁用的东西成为一个很好的理由。你真的应该声明(@property
)并使用(
,而不是->
-即你开始使用的表达式)属性。我得到了一个错误:“语义问题:执行此操作时,实例变量'tweetText'再次受到保护”,但在使用@public
@zad0xsis时,你不应该使用它。”。而不是“->”它会起作用的。使用属性是惯例,访问公共IVAR(带“->”)是违反惯例的
@synthesize nick, tweetText;
@property (nonatomic, retain) IBOutlet UILabel *nick;
@property (nonatomic, retain) IBOutlet UITextView *tweetText;