如何将UITableViewCell行值提取到UIButton操作方法

时间:2014-06-23 07:28:53

标签: ios objective-c uitableview uibutton

您好,在我的应用程序中,我是从服务器获取数据并显示在UITableViewCell中,我的UIButtons中有动态UITableView。现在,我希望将UITableViewCell数据提供给我的UIButton操作方法。

我的代码来获取服务器数据。

 -(void) retrieveData
 {
    NSURL * url = [NSURL URLWithString:getDataURL];

    NSData * data = [NSData dataWithContentsOfURL:url];

    id json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

    menuarray = [[NSMutableArray alloc]init];
    menuarray1=[NSMutableArray array];
    NSArray * respArray = [json objectForKey:@"nonveg_biriyani"];

  for (int i=0; i<respArray.count; i++)
     {

        NSString * pal = [[respArray objectAtIndex:i]objectForKey:@"name"];
        NSString * tll = [[respArray objectAtIndex:i]objectForKey:@"cost"];
       [menuarray addObject:pal];
       [menuarray1 addObject:tll];
     }

   [self.mytableview reloadData];
 }

我的UITableViewCell代码。

  -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
      return 1;
 }
 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     return menuarray.count;
 }

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

     meunuCell *cell =(meunuCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
     if (cell == nil) {

         cell = [[meunuCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
       }

        cell.name.text=[menuarray objectAtIndex:indexPath.row];
        cell.ctt.text=[menuarray1 objectAtIndex:indexPath.row];


        button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button2.frame = CGRectMake(280,20, 40.0, 40.0);
        [button2 setTitle:@"add" forState:UIControlStateNormal];
        [button2 addTarget:self action:@selector(aMethod2:)
        forControlEvents:UIControlEventTouchUpInside];
button1.tag=indexPath.row;
        [cell.contentView addSubview:button2];


        }

   return cell;

  }

UIButton行动方法。

  -(void)aMethod2:(id)sender
 {
    int tag=[sender tag];
    NSString *item=[self.menuarray objectAtIndex:tag];
    NSLog(@"%@",item);
    NSString *cost=[self.menuarray1 objectAtIndex:tag];
    NSLog(@"%@",cost);

 }

我已经使用上面的代码来获取UITableViewCell数据,但它无法显示第一行数据请告诉我在上面的代码中我做错了如何将数据提取到我的{{1}行动方法我必须长时间呆在这里请帮助我。

感谢。

4 个答案:

答案 0 :(得分:0)

您必须在cellForRowAtIndexPath中设置按钮标记:

button2.tag = indexPath.row;

默认值为0所以现在每次在aMethod2:方法中都有0作为标记。

答案 1 :(得分:0)

您的button2没有标记,实际上您正在为button1添加标记,但将目标添加到button2

  

button1.tag = indexPath.row;

尝试更改为:

  

button2.tag = indexPath.row;

答案 2 :(得分:0)

如果您希望每个单元格以相同的方式运行,您只需使用委托方法

即可
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

推进行动。

答案 3 :(得分:0)

直接将UIButton分配为cellforRowAtIndexPath,其他方面你会在按钮操作方法上得到最后一个索引的结果

    UIButton  *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(280,20, 40.0, 40.0);
    [button2 setTitle:@"add" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(aMethod2:)
    forControlEvents:UIControlEventTouchUpInside];
    button2.tag=indexPath.row;
    [cell.contentView addSubview:button2];

您的操作方法为

 -(void)aMethod2:(UIButton*)sender
 {

 NSString *item=[self.menuarray objectAtIndex:[sender tag]];
 NSLog(@"%@",item);
 NSString *cost=[self.menuarray1 objectAtIndex:[sender tag]];
 NSLog(@"%@",cost);

 }