User558
User558

Reputation: 1175

How to get the data into buttons in custom cell?

i have a class called custom cell(.h .m) in that have created 4buttons in that buttons i want to store the data? that data coming from JSON parser. this is my code

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"Cell";
    ContestQATableViewCell *cell = (ContestQATableViewCell *)[tableViewQA dequeueReusableCellWithIdentifier:cellId];
    if (cell==nil)
    {
        NSArray *myNib;
        myNib =[[NSBundle mainBundle]loadNibNamed:@"ContestQATableViewCell" owner:self options:nil];
        cell = (ContestQATableViewCell *)[myNib lastObject];
    }
    NSString *str = [getAnswer1Array objectAtIndex:indexPath.row];
    NSLog(@"%@",str);
    cell.question.text = [getContestQArray objectAtIndex:indexPath.row];
    [UIButton setTitle:cell.answer1 forState:UIControlStateNormal];
    [UIButton setTitle:cell.answer2.userActivity forState:UIControlStateNormal];
    [UIButton setTitle:cell.answer3. forState:UIControlStateNormal];
}

Upvotes: 0

Views: 64

Answers (3)

vaibby
vaibby

Reputation: 1265

change key as per your requirement

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath(NSIndexPath *)indexPath 
{
  static NSString *cellId = @"Cell";
  ContestQATableViewCell *cell = (ContestQATableViewCell *)[tableViewQA dequeueReusableCellWithIdentifier:cellId];
  if (cell==nil)
  {
     NSArray *myNib;
     myNib =[[NSBundle mainBundle]loadNibNamed:@"ContestQATableViewCell" owner:self options:nil];
     cell = (ContestQATableViewCell *)[myNib lastObject];
  }
   NSString *str = [[getAnswer1Array objectAtIndex:indexPath.row]objectForKey:@"answer1"];

   NSString *str1 = [[getAnswer1Array objectAtIndex:indexPath.row]objectForKey:@"answer2"];

   NSString *str2 = [[getAnswer1Array objectAtIndex:indexPath.row]objectForKey:@"answer3"];

   NSString *str3 = [[getAnswer1Array objectAtIndex:indexPath.row]objectForKey:@"answer4"];

  [cell.button1 setText:str forState:UIControlStateNormal];
  [cell.button2 setText:str1 forState:UIControlStateNormal];
  [cell.button3 setText:str2 forState:UIControlStateNormal];
  [cell.button4 setText:str3 forState:UIControlStateNormal];
}

Upvotes: 0

Thuc Pham
Thuc Pham

Reputation: 153

Can you please let us know why do you want to store data on a button ? If you want to access your data when touching on the button, you can add index path to your buttons tag.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"Cell";
    ContestQATableViewCell *cell = (ContestQATableViewCell *)[tableViewQA dequeueReusableCellWithIdentifier:cellId];
    if (cell==nil)
    {
        NSArray * myNib =[[NSBundle mainBundle]loadNibNamed:@"ContestQATableViewCell" owner:self options:nil];
        cell = (ContestQATableViewCell *)[myNib lastObject];
    }
    NSString *str = [getAnswer1Array objectAtIndex:0];
    NSLog(@"%@",str);
    cell.question.text = [getContestQArray objectAtIndex:indexPath.row];
    [cell.answerButton1 setTitle:[getContestAnswerArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];
    cell.answerButton1.tag = indexPath.row;
    [cell.answerButton1 addTarget:self action:@selector(didSelectAnswer:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

- (void)didSelectAnswer:(id)sender {
       UIButton * tSelectedButton = sender;
       NSString * text = [getContestAnswerArray objectAtIndex: tSelectedButton.tag];
 }

I just make it for only one button, you can try to do this for the others

Upvotes: 0

user3182143
user3182143

Reputation: 9589

Follow the below steps

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *customTableIdentifier=@"CustomCell";
  ContestQATableViewCell *cell=(ContestQATableViewCell*)[tableView dequeueReusableCellWithIdentifier:customTableIdentifier];
  if (cell==nil)
  {
    NSArray *nibs=[[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil];
    cell=[nibs objectAtIndex:0];
  }
  cell.btnFirst.titleLabel.text = [NSString stringWithFormat:@"%@",[getContestQArray objectAtIndex:indexPath.row]];

  cell.btnSecond.titleLabel.text = [NSString stringWithFormat:@"%@",[getSecondArray objectAtIndex:indexPath.row]];

  cell.btnThird.titleLabel.text = [NSString stringWithFormat:@"%@",[getThirdArray objectAtIndex:indexPath.row]];

  cell.btnFourth.titleLabel.text = [NSString stringWithFormat:@"%@",[getFourthArray objectAtIndex:indexPath.row]];

  return cell;
}

Upvotes: 1

Related Questions