如何将数据导入自定义单元格中的按钮?

时间:2015-12-08 10:21:23

标签: ios uitableview

我有一个叫做自定义单元格(.h .m)的类,它在我想要存储数据的按钮中创建了4个按钮?来自JSON解析器的数据。 这是我的代码

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

3 个答案:

答案 0 :(得分:1)

按照以下步骤

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

答案 1 :(得分:0)

根据您的要求更改密钥

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

答案 2 :(得分:0)

您能否告诉我们您为什么要将数据存储在按钮上?如果要在触摸按钮时访问数据,可以在按钮标记中添加索引路径。

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

我只为一个按钮制作它,你可以尝试为其他按钮