0

Hi am using 2 UIImageView in a cellrow with tag, each row is displayed sequentially by populating images from an array like (0th image,1st image) for row 0, and (2nd image,3rd image) for row 1 and so on...

i am successful in displaying the image with appropriate tag for each image, but am unsuccessful in getting the tag for image or cell in didrowselect method...i need the tag number to directly pass the tagnumber as array index to detailsegue...Am new to IOS programming, i would like to know if am doing anything wrong here, please suggest a way to get the appropriate tag number...your help in this regard is greatly appreciated, as am struck with only this task to be completed!!!

Pasted my code for your reference: self.tagnumber=0; this code is for displaying even number of images.

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

0

In your cellForRowAtIndexPath method, you set the tag of a UIImageView or the tag of a UILabel.

In your didSelectRowAtIndexPath method, you get the tag of the cell.

Either get the tag of the image view or label that is in the cell, or set the cell`s tag.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • i set a common tag for imageview and label since its a one cell, and they are displayed properly... but am really not sure how to get the tag number of my imageView in cell... – pradeeshb Mar 02 '13 at 23:07
  • Why do you need tags on the image views and labels? Just set the `tagNumber` on the cell. – rmaddy Mar 02 '13 at 23:12
  • since am using only one cell to load 2set of images from an array in a row, am unable to set tag for cell...am really confused, using 2 buttons to load images did solve my problem by accessing [sender tag] in buttonclickevent, but am not sure it is advisable to use such a design!!! – pradeeshb Mar 02 '13 at 23:25
  • Your original question was about getting the tag in the `didSelectRowAtIndexPath` method. This is called when a row is tapped. There is no way, from this method, to determine which part of the cell was tapped. – rmaddy Mar 02 '13 at 23:26
  • i will try with two cells and see if it really solves my problem...thanks for ur reply maddy – pradeeshb Mar 02 '13 at 23:33