简体   繁体   中英

Odd behaviour when using UITableView in a UIViewController that is presented Modally

I have discovered an odd behaviour when putting a UITableView in a UIViewController that is presented Modally (Formsheet)

Fixed due to a coding error : For some reasons the table view is showing 2 sets of labels in each cell when only 1 is set up. The first set shows the original Labels name then directly over top is the coded text. See iPhone4s screen shot.

On the iPad the table view is white even though the table, cells and contentView has been set to clearColor. see iPad screen shot.

iPad screen shot:

在此处输入图片说明

iPhone screen shot:

在此处输入图片说明

This is a test code to see if it was an error in my coding in my main app.

class TestTableView:UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var testTable: UITableView!
var data = ["One", "Two", "Three"]

override func viewDidLoad() {
    super.viewDidLoad()
    self.testTable.delegate = self
    self.testTable.dataSource = self
}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("testCell", forIndexPath: indexPath) as! testTableCell
    cell.testTableCell!.text = self.data[indexPath.row]
    return cell
}
}

class testTableCell:UITableViewCell{

@IBOutlet weak var testTableCell: UILabel!

override func layoutSubviews() {
    super.layoutSubviews()
}
}

Something interesting is that the println(self.data.count) prints out 4 times.

Can anyone see a problem in my code? Or is this a bug?

Any feedback would be helpful..

I have fixed the issue but I'm not sure that this is a bug. For some reason setting the background color of the Cell in IB doesn't work on iPad.

Changing the cell code to this worked.

class testTableCell:UITableViewCell{

@IBOutlet weak var testTableCell: UILabel!

override func layoutSubviews() {
    super.layoutSubviews()
    self.backgroundColor = UIColor.clearColor()
}
}

Has no one else had this problem?

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