【问题标题】:how to load Different Array when table view Section Button Pressed?按下表格视图部分按钮时如何加载不同的数组?
【发布时间】:2014-11-12 09:30:58
【问题描述】:

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

NSDictionary *dict=[self.imagesa objectAtIndex:0];

但是对于第二部分,有两个单元格使用相同的自定义单元格。我现在很困惑,当按下 tableview 的第二部分 PREVIEW 按钮时如何显示第二个对象数组,以及按下第二部分的第二个单元格 Preview 按钮时如何加载第三个图像数组。如果我为每个按钮制作不同的 Xib,那么将来如果添加新单元格,它将无法工作。请给我解决方案,这里是网络服务链接WebService link

我想显示 First -demopage: array for first cell and Second -demopage: for second and up to last cell of table view。怎么可能请给我解决方案。

这是我的预览按钮代码

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;

和它的动作一样

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

但是现在我很困惑,当我的 Section Row Button 在索引处按下时,我如何将演示页面链接数组加载到另一个视图控制器中。

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

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

我为我的 Tableview 单元格按钮设置了一个标签

preview.tag=indexPath.row;

和它的动作一样

-(IBAction)showPreviewSeondSection:(id)sender
{
UIButton *button = (UIButton *)sender;
NSInteger row = button.tag;
for (index=0; index<[self.imagesa count]; index++)
{
if (row == 1)
{
    PreviewViewController *preview=[[PreviewViewController alloc]initWithNibName:@"PreviewViewController" bundle:nil];
    preview.imagesa=self.imagesa;
    [self presentViewController:preview animated:YES completion:nil];
    NSLog(@"Preview array %@",preview.imagesa);
}
}
}

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

【问题讨论】:

  • 您使用多少个数组来加载 2 个部分的数据?
  • 我只使用一个数组,但我的数组子数组包含我想显示给 Xib 文件中每个 tableviewCell 的图像链接。这里我的 Web 服务链接是 truemanindiamagazine.com/webservice/magazine_list.php 在这个链接中我想显示 -demopage : 我要显示的每个单元格的数组值。
  • @AshishGabani:检查我的答案。

标签: ios objective-c arrays uitableview


【解决方案1】:

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

在 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  


}

希望对你有帮助

【讨论】:

    【解决方案2】:

    在您的按钮按下方法中,

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 2023-03-31
    相关资源
    最近更新 更多