如何在表视图部分按钮按下时加载不同的数组?

时间:2014-11-12 09:30:59

标签: ios objective-c arrays uitableview

我是iOS开发的新手我正在开发一个杂志应用程序。我已经使用了包含两个部分的tableview,每个部分都有PREVIEW按钮,如下图所示。 enter image description here我想在按下PREVIEW按钮时显示不同的图像。图像数组是从JSON解析的。这里所有图像都在数组中(此数组包含图像数组)。所以对于第一部分,我写了一个像

这样的代码
NSDictionary *dict=[self.imagesa objectAtIndex:0];

但对于第二部分,两个单元格使用相同的自定义单元格。我现在感到困惑的是,如果在第二部分的第二部分PREVIEW按按下。如果我为每个按钮制作不同的Xib,那么将来如果添加了新的单元格,那么它将无法工作。请给我解决方案,这里是网络服务链接WebService link

我想展示First-demopage:第一个单元格和第二个单元格的数组:表格视图的第二个和最后一个单元格。怎么可能请给我解决方案。

这是我的预览按钮代码

Preview

它的行动就像

UIButton *preview = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        preview.frame = CGRectMake(112,95, 89, 25);
        preview.backgroundColor=[UIColor colorWithRed:229.0/256.0 green:229.0/256.0 blue:229.0/256.0 alpha:1.0f];
        [preview setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [preview setTitleColor:[UIColor colorWithRed:229.0/256.0 green:229.0/256.0 blue:229.0/256.0 alpha:1.0] forState:UIControlStateHighlighted];
        [preview setTitle:@"PREVIEW" forState:UIControlStateNormal];
        [preview.titleLabel setFont:[UIFont fontWithName:@"RobotoCondensed-Bold" size:14.0f]];
        [cellTwo addSubview:preview];
        [preview addTarget:self action:@selector(showPreviewSeondSection:)forControlEvents:UIControlEventTouchUpInside];

        preview.tag=indexPath.row;

但现在我很困惑如何在我的Section Row Button按索引时将我的demopage链接数组加载到另一个视图Controller中。

现在我更新我的问题,因为我首先从主数组中解析了我的-demopage键,如

-(IBAction)showPreviewSeondSection:(id)sender
{
UIButton *button = (UIButton *)sender;
NSInteger row = button.tag;
NSLog(@"Tag of button %d",row);
}

我为我的Tableview Cell Button设置了一个标签

if (responsedata.length > 0)
{
    NSError* error;

    self.json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
    if ([[_json objectForKey:@"data"] isKindOfClass:[NSArray class]])
    {
        NSArray *arr = (NSArray *)[_json objectForKey:@"data"];
        [self.imageArray addObjectsFromArray:arr];
        [self.storeViewTable reloadData];
    }
    self.storeViewTable.hidden=FALSE;
    }
    for (index=0; index<[self.imageArray count]; index++)
    {
        NSDictionary *imgDict = [self.imageArray objectAtIndex:index];
        if([imgDict count]>0)
        {
            NSMutableArray *imgsLinkArray = [imgDict objectForKey:@"demopage"];
            self.imagesa = imgsLinkArray;
        }
    }

及其行为如

preview.tag=indexPath.row;

然后它打印我的-demopage数组我的Scrollview中的最后一个索引请给我解决方案我如何按第一个按钮然后我得到第一个索引值,当我按下第二个单元格按钮然后我想打印第二个索引值。

2 个答案:

答案 0 :(得分:0)

在处理多个部分时可能有所帮助

在cellforrow中执行此操作

yourcell.tag=indexpath.section;
yourcell.contentview.tag=indexpath.row;

并在预览操作中

 -(void)previewbuttonAction:(id)sender
{
   UIButton *button = (UIButton *)sender; // first, cast the sender to UIButton

   id rowid =[button superview];
  id sectionid = [rwoid superview];

   NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[rowid tag] inSection:[sectionid tag]];

   // apply your logic here depending on the array for row and section  


}

希望有所帮助

答案 1 :(得分:0)

按下按钮的方法,

-(IBAction)showPreviewSeondSection:(id)sender
{
      CGPoint rootViewPoint = [sender.superview convertPoint:CGRectZero toView: self.storeViewTable];
      NSIndexPath *indexPath = [self.storeViewTable indexPathForRowAtPoint:rootViewPoint];

     PreviewViewController *preview=[[PreviewViewController alloc]initWithNibName:@"PreviewViewController" bundle:nil];
     preview.imagesa=self.imagesa[indexPath.row];
     [self presentViewController:preview animated:YES completion:nil];

}

您可以获取indexPath然后从您的数组访问该对象并传递到下一个ViewController