[英]how to load Different Array when table view Section Button Pressed?
我是iOS开发的新手,正在开发杂志应用程序。 在那我使用了包含两个部分的tableview,每个部分都有PREVIEW
按钮,如下图所示。 按下“ PREVIEW
按钮时,我想显示不同的图像。 图像数组是从JSON解析的。 这里所有图像都在数组中(此数组包含图像数组)。 因此,对于第一部分,我编写了如下代码
NSDictionary *dict=[self.imagesa objectAtIndex:0];
但是对于第二部分,有两个单元格使用相同的自定义单元格。 现在我很困惑,如何在按下tableview的第二部分的PREVIEW
按钮时如何显示第二个对象数组,以及在按下第二部分的第二个单元格Preview
按钮时以相同的方式加载第三图像数组。 如果我为每个按钮制作了不同的Xib,那么将来如果添加了新单元,它将无法正常工作。 请给我解决方案,这里是Web服务链接WebService链接
我想显示表格视图的第一个-demopage:数组,第二个-demopage:用于第二个及最后一个单元格。 怎么可能请给我解决方案。
这是我的预览按钮代码
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);
}
但是现在我很困惑如何在按索引的“节行”按钮时将我的演示链接链接数组加载到另一个视图控制器中。
现在我更新我的问题,因为我首先从我的主数组中解析了-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 Cell Button设置了一个标签为
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中的最后一个索引,请给我有关如何按第一个按钮然后我得到第一个索引值的解决方案,当我按第二个单元格按钮时我要打印第二个索引值。
在处理多个部分时可能会有所帮助
在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
}
希望能帮助到你
在您的按钮按下方法中,
-(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中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.