Chandu Chandu - 3 months ago 8
iOS Question

Configuring Data in a tableViewCell on a button click

I am configuring a

UITableViewCell
with my coreData attributes. I am able to display the text.But i want to reload the cell with other data on a button click.

-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
Book *entity = [self.fetchedResultsController objectAtIndexPath:indexPath];
firstLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 120, 21)];
secondLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 10, 120, 21)];

firstLabel.text = entity.title;
secondLabel.text = entity.author;

[cell.contentView addSubview:firstLabel];
[cell.contentView addSubview:secondLabel];

//On button Click i want to change firstLabel.text = entity.date and secondlabel.text = entity.details

}

Answer

Try this code..

Set global variale

int buttonCliked;

Set action for button

- (IBAction)aaaa:(id)sender
{
    buttonCliked =1;
    [_myTable reloadData];
}

Load the tableviewCell with data

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

Book *entity = [self.fetchedResultsController objectAtIndexPath:indexPath];

firstLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 120, 21)];
secondLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 10, 120, 21)];

if(!buttonClicked)
{
    firstLabel.text = entity.title;
    secondLabel.text = entity.author;
}
else
{
    firstLabel.text =  entity.date;
    secondLabel.text = entity.details;

    if(indexPath.row >= [tableView numberOfRowsInSection:indexPath.section] && indexPath.section >= [tableView numberOfSections])
    {
        buttonCliked =0;
    }

}

[cell.contentView addSubview:firstLabel];
[cell.contentView addSubview:secondLabel];

return cell;


}
Comments