user3454910
user3454910

Reputation: 89

pass data to UITableViewCell iOS

I currently have a tableView with custom cells that I've called AMPFeedbackTableViewCell. The cells load up perfectly fine.

Here is the code:

-(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];
    }

I'm trying to pass something to the cell before it starts like in this case it's _currentFeedback. The custom cell has a AMPFeedback item and I want it to set it before it loads. So I can use it like this : (NOTE: This is in AMPFeedbackTableViewCell

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

However, _currentFeedback is always nil. Is there a way I can pass it over then call awakeFromNib?

Thanks in advance

Upvotes: 4

Views: 1515

Answers (2)

Ravi Tailor
Ravi Tailor

Reputation: 189

    You can do initialization and loading of data in the following method of custom uitableview cell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
   self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
   if (self) {
       // Initialization code
      }
      return self;
  }

 and you can load customcell in your tableview
 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
       static NSString *eventListCell   = @"EventListCell";
       EventListCell *cell = (EventListCell *)[tableView dequeueReusableCellWithIdentifier:eventListCell];

         if (cell == nil)
        {
               cell = (EventListCell*)[[[NSBundle mainBundle] loadNibNamed:@"EventListCell" owner:self options:nil] objectAtIndex:0];
     // here you can access the custom class public variable to set data .
        }

}

Upvotes: 0

Val
Val

Reputation: 22807

If you are not insist on doing this inside awakefromNib, there's another way (and maybe better) to do this:

in AMPFeedbackTableViewCell.h

@property (strong) AMPFeedBack *currentFeedback;

in AMPFeedbackTableViewCell.m

@synthesize currentFeedback = _currentFeedback;

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

    _currentFeedback = feedback;
    [self loadImages];
}

and then we can trigger it directly in main code:

-(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];

Hope it helps.

Upvotes: 2

Related Questions