AMPFeedbackTableViewCell

AMPFeedbackTableViewCell

我目前有一个带有自定义单元格的 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/

10-14 21:31