UITableViewCell中的项目在上下滚动时会更改其位置

时间:2019-06-06 04:57:01

标签: ios objective-c iphone uitableview uibutton

我正在使用UITableView来显示array的元素。在UITableViewCell中,我使用了多个UILabelsUIButtons来执行动作。一切显示都很好,但是当我尝试scroll TableView时,就像每次每次Images n Labels改变索引位置时开始滚动一样,都会出现问题。这是我第一次面临此类问题。我还通过了许多关于SO的解决方案,但问题仍然相同。

我正在共享我的代码:

    -(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;
    }

我的JSON Array答复是:

{
            "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": ""
        },

2 个答案:

答案 0 :(得分:1)

您的单元格是动态单元格,这意味着表视图单元格的子视图有时被隐藏。那时,标签无法正常工作。您的方法面临问题。此时,您需要将一个单独的类添加到tableview单元格。给出口标签和其他子视图。通过“ cellForRowAtIndex”方法访问此单元类。

答案 1 :(得分:1)

首先,您将获得带有特定标签的按钮

subl

然后您将按钮的标记重写到indexPath.row

sublime_text

当单元被重用时,您将无法再次获得该按钮,因为它的标签现在有所不同。尝试将indexPath.row关联到单元格,然后从sender.superview.tag取回

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