• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:38:03 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我用 XIB 制作了一个自定义单元格: .h

#import <UIKit/UIKit.h>

@interface TWCustomCell : UITableViewCell {
    IBOutlet UILabel *nick;
    IBOutlet UITextView *tweetText;
}

@end

.m

#import "TWCustomCell.h"

@implementation TWCustomCell

- (id)initWithStyleUITableViewCellStyle)style reuseIdentifierNSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelectedBOOL)selected animatedBOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

我以这种方式将它们加载到 cellForRowAtIndexPath: 中:

- (UITableViewCell *)tableViewUITableView *)_tableView cellForRowAtIndexPathNSIndexPath *)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' protected ”。我该怎么办?



Best Answer-推荐答案


您没有声明允许使用点语法访问类外部的 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;

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap