Transfer data to UITableViewCell iOS

I currently have a tableView with custom cells, which I called AMPFeedbackTableViewCell. Cells load 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 am trying to pass something to a cell before it starts, as in this case, _currentFeedback. The custom cell has an AMPFeedback element, and I want it to set it before loading. So I can use it as follows: (NOTE: this is in AMPFeedbackTableViewCell

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

However, the current feedback is always zero. Is there a way to pass this and then call awakeFromNib?

Thanks in advance

+4
source share
2 answers

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

, .

+2
    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 .
        }

}

0

Source: https://habr.com/ru/post/1536736/


All Articles