无法在didrowselect方法中获取图像/单元格标签号...

时间:2013-03-01 21:59:39

标签: uitableview uiimageview ios6.1

您正在带有标签的cellrow中使用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);
}

1 个答案:

答案 0 :(得分:0)

cellForRowAtIndexPath方法中,您可以设置UIImageView的标记或UILabel的标记。

didSelectRowAtIndexPath方法中,您会获得该单元格的标记。

获取单元格中图像视图或标签的标记,或设置单元格标记。