简体   繁体   English

表格视图重叠的单元格swift2

[英]Table view overlapping cells swift2

I have a tableview in my app. 我的应用程序中有一个tableview it's on the detail view of my split view . 它在我的分屏视图详细视图 Whenever i populate my tableview with UItextviews , the cells seem to overlap each other. 每当我用UItextviews填充tableview时,单元格似乎相互重叠。 this is not what i want. 这不是我想要的。 Does anyone have any suggestions to fix this? 有没有人建议解决此问题?

问题的屏幕截图

here's my vc code: 这是我的vc代码:

  import UIKit

class DetailViewController: UIViewController, UITableViewDelegate, UITextViewDelegate {

@IBOutlet weak var messagesTableView: UITableView!
var initVar = 0

var row: Int?

var firstLoad = true

var detailItem = {}
var convo = ["hello", "hellohello", "hellohellohellohello", "hellohellohellohellohellohellohellohello", "hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello", "hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello", "hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello", "hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello", "hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello", "hellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohellohello"]

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    print("hello")
    messagesTableView.separatorColor = UIColor.whiteColor()

    print(row)
    messagesTableView.reloadData()
    print(firstLoad)

    let numberOfSections = messagesTableView.numberOfSections
    let numberOfRows = messagesTableView.numberOfRowsInSection(numberOfSections-1)

    if numberOfRows > 0 {
        print(numberOfSections)
        let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: (numberOfSections-1))
        messagesTableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: false)
    }
    messagesTableView.reloadData()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return convo.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("detailCell", forIndexPath: indexPath)

    var textView = UITextView()

    textView.delegate = self
    textView.editable = false
    textView.text = convo[indexPath.row]
    textView.textAlignment = .Center
    textView.sizeToFit()

    if indexPath.row % 2 == 0 {
        textView = UITextView(frame: CGRect(x: cell.frame.width * (2/5), y: 4, width: cell.frame.width * (3/5), height: cell.frame.height))
        textView.backgroundColor = UIColor.blueColor()
    } else {
        textView = UITextView(frame: CGRect(x: 0, y: 4, width: cell.frame.width * (3/5), height: cell.frame.height))
        textView.backgroundColor = UIColor.greenColor()
    }

    textView.textColor = UIColor.whiteColor()

    textView.delegate = self
    textView.editable = false
    textView.text = convo[indexPath.row]
    textView.textAlignment = .Center
    textView.sizeToFit()
    tableView.rowHeight = textView.frame.height + 7
    cell.addSubview(textView)


    return cell
}
}

The cells are being re-used in a UITableView . 单元格正在UITableView重新使用。

You are adding TextView's on it, so you are keep on adding TextView's every time it's being re-used. 您将在其上添加TextView,因此您将在每次重复使用TextView时继续添加它。

Your code should be modified so that the TextView is only added IF it's not already present on the cell. 你的代码应该修改,以便TextView的只添加如果它不存在于细胞。

The easiest way, is to add a TAG (unique) to the TextView, then if the View from Tag is nil (AKA not present) add it, else get it from the ViewFromTag 最简单的方法是将一个TAG(唯一)添加到TextView中,然后如果Tag中的View为nil(又不存在),则添加它,否则从ViewFromTag中获取它。

As for an example, although I'm still using obj-c and with that in mind, my code might have an error, here is the code: 作为示例,尽管我仍在使用obj-c并牢记这一点,但是我的代码可能有错误,以下是代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("detailCell", forIndexPath: indexPath)

        var textView: UITextView

        if (cell.viewWithTag(10000) != nil) {
            textView = (cell.viewWithTag(10000) as? UITextView)!
        }
        else {
            textView  = UITextView()
            textView.tag = 10000
            textView.delegate = self
            textView.editable = false
            textView.textAlignment = .Center
            cell.addSubview(textView)
        }

        textView.text = convo[indexPath.row]

        if indexPath.row % 2 == 0 {
            textView.frame = CGRectMake(cell.frame.width * (2/5) , 4, cell.frame.width * (3/5), cell.frame.height)
            textView.backgroundColor = UIColor.blueColor()
        } else {
            textView.frame = CGRectMake(0 , 4, cell.frame.width * (3/5), cell.frame.height)
            textView.backgroundColor = UIColor.greenColor()
        }

        textView.sizeToFit()
        tableView.rowHeight = textView.frame.height + 7

        return cell
    }

To explain the code, as you didn't understood: 为了解释代码,您可能不了解:

The UITableView is re-using the cells. UITableView正在重新使用单元格。 This means, whenever a cell is moved outside the visible part of the screen, it is been re-used (as it is) and is displayed as a new cell for the TableView. 这意味着,只要将单元格移到屏幕可见部分之外,它就会被重新使用(按原样)并显示为TableView的新单元格。

The reason for this, is memory efficiency. 这样做的原因是内存效率。 So imagine a UITableView with 1000 row. 因此,想象一个具有1000行的UITableView。 If for each row a new cell was created, we will have to allocate memory for each cell. 如果为每一行创建了一个新的单元格,我们将不得不为每个单元格分配内存。

But in a typical application usually only a few cells are visible each time (usually less than 10). 但是在典型的应用中,通常每次只能看到几个单元格(通常少于10个)。 So the trick here is that we only need to create that many cells. 所以这里的窍门是我们只需要创建那么多单元格。 Once a cell gets out of sight, it is been served as a new cell for the TableView. 一旦某个单元格消失了,它就将用作TableView的新单元格。

So if you add a UITextView every time the tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell delegate method is called, and the cell is been re-used, you end up adding UITextViews to a cell that already has one. 所以,如果你添加UITextView每次时tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell委托方法被调用,并且该单元被重新使用,你最终将UITextViews到已有一个细胞。 So you end up with the problem you showed. 因此,您最终遇到了所显示的问题。

The code I provided does two things differently: 我提供的代码在两方面做的不同:

  1. It does assign a unique Tag number on the UITextView when it's added to the cell ( textView.tag = 10000 ) 当它添加到单元格时,它确实在UITextView上分配了唯一的Tag编号( textView.tag = 10000
  2. It does check if a UITextView has already been added to the cell and if it has been added we use that UITextView and we don't add it again ( if (cell.viewWithTag(10000) != nil) ) 它确实检查是否已将UITextView添加到单元格中,并且是否已添加该UITextView我们将使用该UITextView且不再进行添加( if (cell.viewWithTag(10000) != nil)

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

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