【问题标题】:Unable to get image/cell tag number in didrowselect method...!无法在 didrowselect 方法中获取图像/单元格标记号...!
【发布时间】:2013-03-01 21:59:38
【问题描述】:

您好,我在带有标签的单元格中使用 2 个 UIImageView,每行通过填充数组中的图像顺序显示,例如第 0 行的 (0th image, 1st image) 和第 1 行的 (2nd image, 3rd image) 等等上...

我成功地为每个图像显示带有适当标记的图像,但在 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);
}

【问题讨论】:

    标签: uitableview uiimageview ios6.1


    【解决方案1】:

    在您的cellForRowAtIndexPath 方法中,您设置UIImageView 的标签或UILabel 的标签。

    在您的didSelectRowAtIndexPath 方法中,您将获得单元格的标签。

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

    【讨论】:

    • 我为 imageview 和 label 设置了一个通用标签,因为它是一个单元格,并且它们显示正确......但我真的不确定如何在单元格中获取我的 imageView 的标签号......
    • 为什么需要在图像视图和标签上添加标签?只需在单元格上设置tagNumber
    • 因为我只使用一个单元格从一个数组中连续加载 2 组图像,我无法为单元格设置标签...我真的很困惑,使用 2 个按钮加载图像确实解决了我的问题通过在buttonclickevent中访问[sender tag],但不确定是否建议使用这样的设计!!!
    • 您最初的问题是关于在didSelectRowAtIndexPath 方法中获取标签。当点击一行时调用它。无法通过这种方法确定单元格的哪一部分被窃听。
    • 我将尝试使用两个单元格,看看它是否真的解决了我的问题...感谢您的回复 maddy
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 2013-04-05
    • 2018-06-24
    • 2020-08-16
    相关资源
    最近更新 更多