简体   繁体   English

CustomCell中的ScrollView上的Swift按钮彼此重叠

[英]Swift- Buttons on ScrollView in CustomCell overlapping each other

I have created a custom cell in the storyboard, and added a ScrollView in it. 我在情节提要中创建了一个自定义单元,并在其中添加了ScrollView。 Now, what I want to do is, create multiple buttons based on the data from my webservice. 现在,我要做的就是根据我的Web服务中的数据创建多个按钮。 Below is the code for that: 以下是该代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: MainTableViewCell! = tableView.dequeueReusableCellWithIdentifier("cellIdentifier", forIndexPath: indexPath) as! MainTableViewCell

    cell.titleLabel.text = self.postsData.objectAtIndex(indexPath.row).valueForKey("title") as? String
    let imageurl = self.postsData.objectAtIndex(indexPath.row).valueForKey("image") as? String
    cell.titleImage.load(imageurl!)

    let jsonStringAsArray = self.postsData.objectAtIndex(indexPath.row).valueForKey("links")
    let data: NSData = jsonStringAsArray!.dataUsingEncoding(NSUTF8StringEncoding)!
    let anyObj: AnyObject? = try! NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0))
    var yCoord:CGFloat = 15.0, xCoord:CGFloat = 0.0, buttonHeight:CGFloat = 30.0, buttonWidth:CGFloat = 100.0, buffer:CGFloat = 10.0,i = 0;

    while i<anyObj?.count {
        if cell.viewWithTag((indexPath.row+1)*1000+(i+1)) != nil{
            print("fake")

        }
        else{
            let button :UIButton = UIButton()
            button.frame = CGRectMake(xCoord, yCoord, buttonWidth, buttonHeight)


            button.setTitleColor(UIColor.greenColor(), forState: .Normal)
            button.layer.borderWidth = 1.0
            button.layer.borderColor = UIColor.greenColor().CGColor
            button.layer.cornerRadius = 10.0
            button.titleLabel?.font = UIFont(name: "Futura-Medium", size: 17)
            button.tag = (indexPath.row+1)*1000+(i+1);
            print(button.tag)
            button.addTarget(self, action: #selector(MainViewController.linkButtonAction(_:)), forControlEvents: .TouchUpInside)
            button.setTitle(anyObj?.objectAtIndex(i).valueForKey("title") as? String, forState: .Normal)
            button.sizeToFit()
            let tempWidth = button.frame.size.width + 10;
            button.frame=CGRectMake(xCoord, yCoord, tempWidth, buttonHeight)

            cell.buttonScrollView.addSubview(button)


            xCoord += button.frame.size.width + buffer;

        }
        i += 1
    }
    cell.buttonScrollView.contentSize = CGSizeMake(xCoord, buttonHeight+10)

    cell.clipsToBounds = true;

    return cell
}

The code I have written, seems to work perfectly, except the problem is that buttons in the first and the last cell are overlapped with buttons from the other cells. 我编写的代码似乎运行良好,只是问题是第一个和最后一个单元格中的按钮与其他单元格中的按钮重叠。

The first cell displays all buttons perfectly at first, but when I scroll down to the bottom and come back up, its all messed up. 首先,第一个单元格完美地显示了所有按钮,但是当我向下滚动到底部并返回时,所有按钮都弄乱了。

It is supposed to appear like this on all cells: 应该在所有单元格上都这样显示: 第二单元

But appears like this: 但是看起来像这样:

拳头牢房

I tried checking for views with same tags in the cell, and clipsToBounds, as you can see in the code, but nothing worked for me. 正如您在代码中看到的那样,我尝试检查单元格中具有相同标签的视图以及clipsToBounds,但是对我没有任何帮助。

Any help appreciated. 任何帮助表示赞赏。 Thanks! 谢谢!

What you are doing is adding the button subviews to a cell that was already configured. 您正在做的是将按钮子视图添加到已配置的单元格中。 You will need to remove existing buttons from the cell after you dequeueReusableCellWithReuseIdentifier:forIndexPath and re-create them again. 使dequeueReusableCellWithReuseIdentifier:forIndexPath并重新创建它们之后,需要从单元中删除现有按钮。

your code see me: your code write button code else but if condition does not any button related code. 您的代码看到了我:您的代码写了按钮代码,但是如果条件没有任何与按钮相关的代码。

Try my adviser of you also write code in if condition in opposite of else condition. 试试我的顾问,如果条件与其他条件相反,也可以编写代码。 after check some time you are not erase you else condition code that you are scrolling you controller that item is overlapping that write erase code button in if condition. 检查一段时间后,您将不会擦除其他条件代码,而您正在滚动的条件代码将控制该项目与if条件下的写入擦除代码按钮重叠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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