【问题标题】:Items in UITableViewCell changes its position while scrolling upside and downsideUITableViewCell 中的项目在上下滚动时更改其位置
【发布时间】:2019-10-21 14:09:22
【问题描述】:

我正在使用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;
    }

我的JSONArray回复是:

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

【问题讨论】:

  • 细胞被重复使用。你做了一个if (...) else if (...) {} ...,你需要在每个条件下为每个设置隐藏属性。在末尾添加else {}

标签: ios objective-c iphone uitableview uibutton


【解决方案1】:

您的单元格是动态单元格,这意味着有时表格视图单元格的子视图会被隐藏。那时标签不起作用。您的方法面临问题。这时候你需要给tableview cell单独取一个类。给网点标签和其他子视图。在“cellForRowAtIndex”方法中访问这个单元格类。

【讨论】:

  • 我正在尝试这个,但是如何处理按钮操作,因为我必须在一列内放置 4 个按钮,并且我必须在 tableview 单元格内创建 5 个列你有任何代码示例来处理操作吗在tableviewcell里面
  • 是的。我已经在 myProject 中处理了这些类型的事情。现在我无法分享代码。所以给 btn.tag = indexpath.row;那么你就可以得到答案了。
【解决方案2】:

首先你会得到带有特定标签的按钮

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

然后你将按钮的标签重写为 indexPath.row

tickButton1.tag = indexPath.row;

当单元格被重复使用时,您无法再次获得按钮,因为它的标签现在不同了。尝试将您的 indexPath.row 分配给一个单元格,然后从 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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    相关资源
    最近更新 更多