ios - 将数据传递给 UITableViewCell iOS

标签 ios objective-c uitableview

我目前有一个带有自定义单元格的 tableView,我称之为 AMPFeedbackTableViewCell。细胞加载得很好。

代码如下:

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"feedbackCell"];

    AMPFeedbackTableViewCell* cell = (AMPFeedbackTableViewCell*) [tableView dequeueReusableCellWithIdentifier:@"feedbackCell"];
    if (cell == nil)
    {
        cell = [[AMPFeedbackTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"feedbackCell"];
        cell.currentFeedback = [[AMPFeedback alloc] init];
        cell.currentFeedback = [_feedbackArray objectAtIndex:indexPath.row];
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FeedbackTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

我试图在它开始之前向单元格传递一些东西,就像在这种情况下它是 _currentFeedback。自定义单元格有一个 AMPFeedback 项目,我希望它在加载之前对其进行设置。所以我可以像这样使用它:(注意:这是在 AMPFeedbackTableViewCell 中

- (void)awakeFromNib
{
    // Initialization code
    if (_currentFeedback != nil)
        [self loadImages];
}

但是,_currentFeedback 始终为零。有什么方法可以传递它然后调用 awakeFromNib?

提前致谢

最佳答案

如果您不坚持在 awakefromNib 中执行此操作,还有另一种方法(也许更好):

在 AMPFeedbackTableViewCell.h 中

@property (strong) AMPFeedBack *currentFeedback;

在 AMPFeedbackTableViewCell.m 中

@synthesize currentFeedback = _currentFeedback;

- (void)setCurrentFeedback:(AMPFeedback *)feedback {
    if (feedback==nil) return;

    _currentFeedback = feedback;
    [self loadImages];
}

然后我们就可以在主代码中直接触发了:

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    AMPFeedbackTableViewCell* cell = (AMPFeedbackTableViewCell*) [tableView dequeueReusableCellWithIdentifier:@"feedbackCell"];
    if (cell == nil) {
        cell = [[AMPFeedbackTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"feedbackCell"];
    }

    cell.currentFeedback = [_feedbackArray objectAtIndex:indexPath.row];

希望对您有所帮助。

关于ios - 将数据传递给 UITableViewCell iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23103735/

相关文章:

iphone - NSData dataWithContentsOfFile 仪器 100% 泄漏

ios - 如何在 Apple Pay PKPaymentAuthorizationViewController 上方显示 UIAlertView?

objective-c - Objective-C : Background & Frame Issue During Orientation Change

ios - InputAccessoryView 在 UITextview resignFirstResponder 上导致 EXC_BAD_ACCESS

ios - 在 iOS 中出现键盘时启用滚动内容

ios - 从 Crashlytics 3.0.0 开始,实现用户提示是应用程序的责任

ios - swift:长按手势识别器不起作用

ios - 自动调整 UITableView 的 header

ios - 从自定义 UITableViewCell 获取数据并返回到 UITableViewController

ios - 展开 2 个 View Controller 的转场