嗨,我在带有标签的单元格中使用 2 UIImageView,通过从数组中填充图像(例如第 0 行的(第 0 图像,第 1 图像)和第 1 行的(第 2 图像,第 3 图像)等数组来按顺序显示每一行。 .
我成功地为每个图像显示了带有适当标签的图像,但在 didrowselect 方法中获取图像或单元格的标签失败...我需要标签号直接将标签号作为数组索引传递给 detailsegue...我IOS编程新手,我想知道这里是否做错了什么,请提出一种获取适当标签号的方法...非常感谢您在这方面的帮助,因为只有这个任务才能完成! !!
粘贴我的代码供您参考:self.tagnumber=0; 此代码用于显示偶数个图像。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//Check Even Rows or odd rows
if(_objects.count%2==0)
{
return (_objects.count/2);
}
else
{
return ((_objects.count/2)+1);
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyCellIdentifier = @"MyCellIdentifier";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyCellIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyCellIdentifier];
}
NSInteger itemcount = _objects.count;
NSLog(@"RowCount:%d",_objects.count/2);
if(itemcount%2==0)
{
NSLog(@"Image Count is Even!");
if(itemcount!=self.tagnumber)
{
RSSItem *object = _objects[self.tagnumber];
UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 4, 145, 95)];
imageView1.tag = self.tagnumber;//1;
NSLog(@"Tag number for Image1:%d", self.tagnumber);
imageView1.image = [UIImage imageWithData:object.artimageData];
[imageView1 setContentMode:UIViewContentModeScaleAspectFill];
[imageView1 setClipsToBounds:YES];
[cell.contentView addSubview:imageView1];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 145, 30)];
label1.tag = self.tagnumber;
[label1 setFont:[UIFont fontWithName:@"AppleGothic" size:14.0]];
[label1 setTextColor:[UIColor blackColor]];
[label1 setContentMode:UIViewContentModeScaleToFill];
[label1 setTextAlignment:NSTextAlignmentCenter];
label1.text = @"Added 2days ago";;
[label1 setNumberOfLines:1];
[cell.contentView addSubview:label1];
//Display next Image
self.tagnumber++;
object = _objects[self.tagnumber];
UIImageView *imageView2= [[UIImageView alloc]initWithFrame:CGRectMake(165, 4, 145, 95)];
imageView2.tag =self.tagnumber;
NSLog(@"Tag number for Image2:%d", self.tagnumber);
imageView2.image = [UIImage imageWithData:object.artimageData];
[imageView2 setContentMode:UIViewContentModeScaleAspectFill];
[imageView2 setClipsToBounds:YES];
[cell.contentView addSubview:imageView2];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(165, 100, 145, 30)];
label2.tag = self.tagnumber;
[label2 setFont:[UIFont fontWithName:@"AppleGothic" size:14.0]];
[label2 setTextColor:[UIColor blackColor]];
[label1 setContentMode:UIViewContentModeScaleToFill];
[label2 setTextAlignment:NSTextAlignmentCenter];
label2.text = @"Added 3days ago";;
[label2 setNumberOfLines:1];
[cell.contentView addSubview:label2];
self.tagnumber++;
[self dismissMegaAnnoyingPopup];
return cell;
}
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Hide Category view if not
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
//[self performSegueWithIdentifier:@"ShowGalleryDetail" sender:cell];
int rownumber = cell.tag;
//Here i get 0 for rownnumber and even if i use indexpath.row**
NSLog (@"Current Page for article is:%i", rownumber);
}