【问题标题】:UITableView is Looping Through Cells (That I Don't Want it to)UITableView 循环通过单元格(我不希望它这样做)
【发布时间】:2016-04-01 04:37:29
【问题描述】:

我正在开发一个主从应用程序,其中我使用带有图像和其他一些细节的自定义原型单元来对事物进行分类。该系统运行良好,直到您制作超过八个细胞。在您达到这一点后,应用程序开始“循环”单元格。我不知道单元格是被复制还是只是重新显示,但如果你删除相邻的单元格,迫使它们变得不同于相隔八个单元格,有时另一个克隆会消失。好像他们纠缠在一起什么的。

自定义单元格类:

import UIKit

class UITableViewToolCell: UITableViewCell {
@IBOutlet weak var thumbnailImage: UIImageView!
@IBOutlet weak var categoryImage: UIImageView!
@IBOutlet weak var sellerImage: UIImageView!

@IBOutlet weak var itemTitle: UILabel!
@IBOutlet weak var itemLocation: UILabel!

var name = "New Item"
var location = "Unknown"
var soldBy = "nil"
var category = "nil"
var thumbnail = "nil"
var information = "Information Not Available"

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    name += " \(arc4random_uniform(5000))"
    itemTitle.text = name
    itemLocation.text = location
    sellerImage.image = UIImage(named: soldBy)
    categoryImage.image = UIImage(named: category)

    let image : UIImageView = thumbnailImage
    image.layer.borderWidth=2.0
    image.layer.masksToBounds = false
    image.layer.borderColor = UIColor.whiteColor().CGColor
    image.layer.cornerRadius = 13
    image.layer.cornerRadius = image.frame.size.height/2
    image.clipsToBounds = true
    image.image = UIImage(named: thumbnail)
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

主视图控制器:

import UIKit

class MasterViewController: UITableViewController , UISearchBarDelegate, UISearchDisplayDelegate {

var detailViewController: DetailViewController? = nil
var objects = [UITableViewToolCell]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem()
    tableView.rowHeight = 111
    let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "insertNewObject:")
    UINavigationBar.appearance().barTintColor = UIColor.purpleColor()
    tableView.backgroundColor = UIColor(red:0.18, green: 0.18, blue: 0.18, alpha: 1.0)
    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont.systemFontOfSize(18, weight: UIFontWeightRegular)], forState: UIControlState.Normal)
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont.systemFontOfSize(18, weight: UIFontWeightRegular)]
    addButton.tintColor = UIColor.whiteColor();
    self.navigationItem.rightBarButtonItem = addButton
    if let split = self.splitViewController {
        let controllers = split.viewControllers
        self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
    }
}

override func viewWillAppear(animated: Bool) {
    self.clearsSelectionOnViewWillAppear = self.splitViewController!.collapsed
    super.viewWillAppear(animated)
}

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

func insertNewObject(sender: AnyObject) {
    let t = UITableViewToolCell()
    objects.insert(t, atIndex: 0)
    let indexPath = NSIndexPath(forRow: 0, inSection: 0)
    self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}

// MARK: - Segues

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {
        if let indexPath = self.tableView.indexPathForSelectedRow {
            let object = objects[indexPath.row]
            let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController

            controller.detailItem = object
            controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
            controller.navigationItem.leftItemsSupplementBackButton = true
        }
    }
}

// MARK: - Table View

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("ToolCell", forIndexPath: indexPath) as! UITableViewToolCell
    objects[indexPath.row] = cell
    let backgroundView = UIView()
    backgroundView.backgroundColor = UIColor(red: 0.3, green: 0.3, blue: 0.3, alpha: 1.0)
    cell.selectedBackgroundView = backgroundView
    return cell
}

override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return false if you do not want the specified item to be editable.
    return true
}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        objects.removeAtIndex(indexPath.row)
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
    } else if editingStyle == .Insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}

}

感谢任何和所有的帮助。

【问题讨论】:

    标签: ios swift loops tableview master-detail


    【解决方案1】:

    我认为您需要在自定义单元格中实现:

    func prepareForReuse() {
        super.prepareForReuse()
        itemTitle.text = ""
        //... and so on
    }
    

    只需在此方法中清理单元格的字段即可。

    【讨论】:

    • 这似乎有效,但我的问题仍然存在:如果我这样做,每次在屏幕底部滚动一个单元格时,它都会调用这个函数并清除它的所有属性,所以它是名称、图标和其他所有内容都替换为默认值。我在该方法中所做的只是将变量重置为其初始值并调用 awakefromnib() 将变量推送到它们的对象中。
    猜你喜欢
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-09
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    相关资源
    最近更新 更多