简体   繁体   中英

Items in UITableViewCell changes its position while scrolling upside and downside

I am using UITableView to display elements of an array . In UITableViewCell i used a number of UILabels and UIButtons to perform actions. Everything displayed well but the problem arise when i try to scroll the TableView as when i start scrolling each time some Images n Labels changes their index position. This is first time i am facing this type of issue. I also gone through many Solutions on SO but the issue remains same.

I am sharing my code:

    -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *simpleTableIdentifier = @"SimpleTableItem";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SimpleTableItem"];
        }
        cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;

        NSString* fromDate = [[eventArray objectAtIndex:indexPath.row]valueForKey:@"StartDateTime1"];

        UILabel * nameLbl = (UILabel *)[cell viewWithTag:101];
        nameLbl.text = fromDate;

     //First Column

        NSString* titleStr = [[eventArray objectAtIndex:indexPath.row]valueForKey:@"Name1"];
        //NSLog(@"%@",spk_about);
        UILabel * spk_aboutLbl = (UILabel *)[cell viewWithTag:102];
        spk_aboutLbl.text = titleStr;


        NSString* Description = [[eventArray objectAtIndex:indexPath.row]valueForKey:@"Description1"];
        NSLog(@"%@",Description);
        UILabel * DescriptionLbl = (UILabel *)[cell viewWithTag:103];
        DescriptionLbl.text = Description;

        NSString* ID = [[eventArray objectAtIndex:indexPath.row]valueForKey:@"ID1"];
        //NSLog(@"-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=%@",compStr);
        UILabel * IDStrLbl = (UILabel *)[cell viewWithTag:104];
        IDStrLbl.text = ID;


        NSString * eventStatus = [[eventArray objectAtIndex:indexPath.row]valueForKey:@"Status1"];
        NSLog(@"eventStatus is ..................................................%@",eventStatus);
        UILabel * eventStatusLbl = (UILabel *)[cell viewWithTag:113];
        eventStatusLbl.text = eventStatus;


        rsvpbtn = (UIButton *)[cell viewWithTag:111];
        [rsvpbtn addTarget:self action:@selector(ShowRSVP:) forControlEvents:UIControlEventTouchUpInside];
        [rsvpbtn setTintColor:[UIColor blackColor]];
        rsvpbtn.tag = indexPath.row;


        mapButton = (UIButton *)[cell viewWithTag:112];
        [mapButton addTarget:self action:@selector(ShowMap:) forControlEvents:UIControlEventTouchUpInside];
        [mapButton setTintColor:[UIColor blackColor]];
        mapButton.tag = indexPath.row;

        //131
        tickButton1 = (UIButton *)[cell viewWithTag:131];
        [tickButton1 addTarget:self action:@selector(tickDetail1:) forControlEvents:UIControlEventTouchUpInside];
        [tickButton1 setTintColor:[UIColor blackColor]];
        tickButton1.tag = indexPath.row;

        if ([eventStatus isEqualToString:@"0"])
        {
            rsvpbtn.hidden = true;
            tickButton2.hidden = true;
        }
        else if ([eventStatus isEqualToString:@"1"])
        {
            tickButton1.hidden = true;
            rsvpbtn.hidden = false;
        }
        else if ([eventStatus isEqualToString:@"2"])
        {
            rsvpbtn.hidden = true;
            tickButton1.hidden = false;
        }
        else if ([eventStatus isEqualToString:@""])
        {
            rsvpbtn.hidden = true;
            mapButton.hidden = true;
            tickButton1.hidden = true;
        }

        return cell;
    }

my JSON Array response is:

{
            "ID1": "11",
            "Name1": "Registration",
            "Description1": "",
            "StartDateTime1": "8:00 AM",
            "EndDateTime1": "8:30 AM",
            "AgendaMapLink1": "",
            "Capacity1": "",
            "AgendaMap1": "",
            "RSVP1": "",
            "TotalGuest1": "1",
            "Status1": "2",
            "ID2": "",
            "Name2": "",
            "Description2": "",
            "StartDateTime2": "",
            "EndDateTime2": "",
            "AgendaMapLink2": "",
            "Capacity2": "",
            "AgendaMap2": "",
            "RSVP2": "",
            "TotalGuest2": "",
            "Status2": "",
            "ID3": "",
            "Name3": "",
            "Description3": "",
            "StartDateTime3": "",
            "EndDateTime3": "",
            "AgendaMapLink3": "",
            "Capacity3": "",
            "AgendaMap3": "",
            "RSVP3": "",
            "TotalGuest3": "",
            "Status3": "",
            "ID4": "",
            "Name4": "",
            "Description4": "",
            "StartDateTime4": "",
            "EndDateTime4": "",
            "AgendaMapLink4": "",
            "Capacity4": "",
            "AgendaMap4": "",
            "RSVP4": "",
            "TotalGuest4": "",
            "Status4": "",
            "ID5": "",
            "Name5": "",
            "Description5": "",
            "StartDateTime5": "",
            "EndDateTime5": "",
            "AgendaMapLink5": "",
            "Capacity5": "",
            "AgendaMap5": "",
            "RSVP5": "",
            "TotalGuest5": "",
            "Status5": ""
        },

Your cell is dynamic cell, means some times subviews of table view cells are hidden. At that time tags not working. You are facing problem with your approach. At this time you need to take a separate class to tableview cell. Give outlets labels and other subviews. Access this cell class in "cellForRowAtIndex" method.

First you are getting the button with specific tag

tickButton1 = (UIButton *)[cell viewWithTag:131];

Then you are rewriting button's tag to an indexPath.row

tickButton1.tag = indexPath.row;

When cell is being reused you can't get the button again, since its tag is now different. Try assingning your indexPath.row to a cell and then getting it back from sender.superview.tag

    if (!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SimpleTableItem"];
    }
    cell.tag = indexPath.row;

- (void) ShowRSVP:(UIButton*) sender{
    NSInteger originalTag = sender.superview.tag;
    //Do stuff
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM