重新加载表格视图后的急动作

时间:2015-08-08 13:46:41

标签: ios swift uitableview button scroll

每当我重新加载行并向上滚动时,我都会得到一个奇怪的抽搐动作。如果它向下滚动它会很好,但向上滚动会导致可怕的抽搐效果,使应用无用。

我不仅附加了cellforrowatindex路径的代码,还附加了按钮。

行的单元格:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!, object: PFObject!) -> PFTableViewCell! {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell
    cell.nopebutton.tag = indexPath.row
    cell.nicebutton.tag = indexPath.row
    cell.killText.text = object.valueForKey("text") as! String
    cell.layoutMargins = UIEdgeInsetsZero
    cell.killText.numberOfLines = 0
    let score = object.valueForKey("count") as! Int
    cell.count.text = "\(score)"
    if cell.count.text == "\(0)"
    {
        cell.count.textColor = UIColor.grayColor()
    }
    if cell.count.text > "\(0)"
    {
        cell.count.textColor = UIColor(red: 42.0/255, green: 204.0/255, blue: 113.0/255, alpha: 1)
    }
    if cell.count.text < "\(0)"
    {
        cell.count.textColor = UIColor(red: 231.0/255, green: 76.0/255, blue: 50.0/255, alpha: 1)
    }
    if cell.count.text == "\(50)"
    {
        cell.count.textColor = UIColor(red: 249.0/255, green: 191.0/255, blue: 59.0/255, alpha: 1)
    }


    if let dict : NSDictionary = NSUserDefaults.standardUserDefaults().objectForKey("userNiceNopeDictionary") as? NSDictionary {
        cell.nicebutton.enabled = true
        cell.nopebutton.enabled = true
        if let nice  = dict[object.objectId] as? Bool{
            if nice {
                cell.nicebutton.enabled = false
            }
            else {
                cell.nopebutton.enabled = false
            }
        }
    }

    let dateUpdated = object.createdAt as NSDate
    let dateFormat = NSDateFormatter()
    dateFormat.dateFormat = "h:mm a"
    cell.time.text = (NSString(format: "%@", dateFormat.stringFromDate(dateUpdated)) as String) as String
    let replycnt = object.objectForKey("replies") as! Int

    if cell.count.text == "\(-10)"
    {
        object.deleteInBackground()
    }


    return cell

}

按钮

@IBAction func topButton(sender: AnyObject) {

  var button = sender as! UIButton
//        var view = button.superview
//        
//        var otherButton = view?.viewWithTag(102) as! UIButton
//        var label = button.superview!.viewWithTag(110) as! UILabel
//        otherButton.enabled = true
//        button.enabled = false

    var rowNumber = button.tag

    var mutableDict = NSMutableDictionary()
    if let dict = NSUserDefaults.standardUserDefaults().objectForKey("userNiceNopeDictionary") {
        mutableDict = dict.mutableCopy() as! NSMutableDictionary
    }

    let obj = self.objects[rowNumber] as! PFObject

    mutableDict.setValue(true, forKey: obj.objectId)
    NSUserDefaults.standardUserDefaults().setObject(mutableDict, forKey: "userNiceNopeDictionary")
    NSUserDefaults.standardUserDefaults().synchronize()
    let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
    let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint)
    let object = objectAtIndexPath(hitIndex)
    object.incrementKey("count")
    //label.text = object.objectForKey("count") as! String
//        self.tableView.reloadData()
    object.save()
    self.tableView.reloadRowsAtIndexPaths([hitIndex!], withRowAnimation: UITableViewRowAnimation.None)
}


@IBAction func bottomButton(sender: AnyObject) {

    var button = sender as! UIButton
    var rowNumber = button.tag

    var mutableDict = NSMutableDictionary()
    if let dict = NSUserDefaults.standardUserDefaults().objectForKey("userNiceNopeDictionary") {
        mutableDict = dict.mutableCopy() as! NSMutableDictionary
    }

    let obj = self.objects[rowNumber] as! PFObject

    mutableDict.setValue(false, forKey: obj.objectId)
    NSUserDefaults.standardUserDefaults().setObject(mutableDict, forKey: "userNiceNopeDictionary")
    NSUserDefaults.standardUserDefaults().synchronize()

    let hitPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
    let hitIndex = self.tableView.indexPathForRowAtPoint(hitPoint)
    let object = objectAtIndexPath(hitIndex)
    object.incrementKey("count", byAmount: -1)
    self.tableView.reloadRowsAtIndexPaths([hitIndex!], withRowAnimation: UITableViewRowAnimation.None)
    object.save()

}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if(segue.identifier == "killDetail"){
        let indexPath = self.tableView.indexPathForSelectedRow()
        let obj = self.objects[indexPath!.row] as! PFObject
        let detailVC = segue.destinationViewController as! DetailViewController
        detailVC.kill = obj
    }
}

}

我的印象是我的身高存在问题,所以我也包含了这些代码。

 override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.separatorInset = UIEdgeInsetsZero
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
    self.tableView.separatorColor = UIColor(red: 236.0/255, green: 240.0/255, blue: 241.0/255, alpha: 1)
    navigationController?.tabBarController?.tabBar.hidden = true
    tabBarController?.tabBar.hidden = true
    self.tableView.estimatedRowHeight = 60
    self.tableView.rowHeight = UITableViewAutomaticDimension
    locationManager.desiredAccuracy = 1000
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
}

我不能为我的生活找出导致这个混蛋的原因。就像我说的那样,我认为它可能与行高的自动尺寸有关,但是当我改变时,问题仍然很明显。

1 个答案:

答案 0 :(得分:0)

  1. 每次出列单元格时,都不会从用户默认值中读取该字典。在tableView(cellForRowAtIndexPath:之外做一次。在viewDidLoad或其他内容中执行此操作。从内存中取出一个元素一旦它在内存中就可以了......虽然每次单元格出列时你都不想从磁盘(或闪存,我猜)中读取。

    如果由于任何原因,在其他地方更改字典并且您希望保持一致状态,请一次读取字典并在控制器/视图控制器之间传递它(通过例如prepareForSegue )并在用户默认值发生变化时将其写入。

  2. 日期格式化程序可能不是一个大的或复杂的对象,但你只需要制作一次,然后可以重复使用它,所以我建议你也只创建一次。

  3. 编辑:如果您只是向上滚动有问题,可能是估计的行高与实际的行高不匹配。尝试删除估计的高度。