更改TableView Scope外部的元素值

时间:2014-08-08 11:11:11

标签: ios uitableview swift ios8

我有一个带有customCell的TableView。我在cellForRowAtIndexPath:中设置了customCell Elements的值。没问题。但我想在cellForRowAtIndexPath:范围之外更改一些customCell元素值。例如,在滑动之后,我想要更改滑动功能中的单元格元素的值

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {

    let cell:customCell = self.tableView?.dequeueReusableCellWithIdentifier("customCell")! as customCell
    let rowData: NSDictionary = self.tableData[indexPath.row] as NSDictionary
    let imageSwipeLeft = UISwipeGestureRecognizer(target: self, action: "imageSwiped:")
        imageSwipeLeft.direction = .Left
    let urlString: NSString = rowData["testImage"] as NSString

    self.indexPathArray += [indexPath]

    cell.testLabel.text = "Test Label"

    cell.testImage.image = image
    cell.testImage.tag = indexPath.row
    cell.testImage.addGestureRecognizer(imageSwipeLeft)
    cell.testImage.userInteractionEnabled = true

    ImageLoader.sharedLoader.imageForUrl(urlString, completionHandler:{(image: UIImage?, url: String) in
        cell.testImage.image = image
        cell.testImage.layer.borderWidth = 6;
        cell.testImage.layer.borderColor = UIColor.whiteColor().CGColor
        cell.testImage.clipsToBounds = true
        cell.placeholderLoading.stopAnimating()
    })

    return cell
}


func imageSwiped(recognizer: UISwipeGestureRecognizer) {
   let testData: NSDictionary = self.tableData[recognizer.view.tag] as NSDictionary
   let imageSlide = recognizer.view as UIImageView

   var imageURL = testData["image"] as String
   ImageLoader.sharedLoader.imageForUrl(imageURL, completionHandler:{(image: UIImage?, url: String) in
        UIView.transitionWithView(imageSlide,
           duration:0.44,
           options: .TransitionCrossDissolve,
           animations: { imageSlide.image = image },
           completion: nil)
   })

   let indexPath = self.indexPathArray[recognizer.view.tag] as NSIndexPath
   let cell = self.tableView?.cellForRowAtIndexPath(indexPath)as customCell
   cell.testLabel.text = "Test"
}

2 个答案:

答案 0 :(得分:1)

UITableView遵循模型 - 视图 - 控制器模式。根据此模式,您需要对模型进行所有更改 - 换句话说,需要对存储填充单元数据的信息的数据结构进行更改。对模型进行更改后,您会告诉视图数据已更改,然后会显示新数据。

假设您的cellForRowAtIndexPath函数从数组中读取。然后,您的imageSwiped函数应找到已刷过的项目,修改其在数组中的条目,然后调用reloadDatareloadRowsAtIndexPaths

就是这样!一旦您通知重新加载的表视图,它将返回到阵列,找到修改后的数据,并致电cellForRowAtIndexPath以显示它。

具体来说,在您的代码中,将一个名为swipedCells的数组添加到您声明tableData数组的同一个类中:

var swipedCells = Boolean[](count:self.tableData.count, repeatedValue: false)

现在替换

cell.testLabel.text = "Test Label"

对齐
if self.swipedCells[indexPath.row] {
    cell.testLabel.text = "Swiped!"
} else {
    cell.testLabel.text = "Test Label"
}

最后,按如下方式更改imageSwiped

let indexPathRow = self.indexPathArray[recognizer.view.tag] as Int
self.swipedCells[indexPathRow] = true
self.tableView?.reloadData()

这样,即使滚动后,您刷过的单元格也会继续有标签"Swiped!"

答案 1 :(得分:0)

很长时间后好了:D我解决了我的问题。

let indexPath = self.priceObjects[recognizer.view.tag] as NSIndexPath
let cell = self.tableView?.cellForRowAtIndexPath(indexPath)as customCell

我在imageSwipe函数中调用它。在cellForRowAtIndexPath函数中,我设置初始单元格元素值,我将indexPath存储在数组中。如果滑动事件发生,我会标记图像以获取索引。我认为它远离理想的方式......但对我有用。我希望有一天会有人发布好方法;)因为这很快就会变脏。