【问题标题】:When I Scroll the tableView, how can I know which cell is touch the Green Area当我滚动 tableView 时,我怎么知道哪个单元格触摸了绿色区域
【发布时间】:2017-12-02 17:03:02
【问题描述】:

The Screen In My Prototype

我的问题是基于The image in the link。因为我的名气不够,这里不能发图

我们假设图像中的绿色区域固定的
而且,我的要求是当 a cell 包含 GA 时,that cell'saudioPlayer 会说出单元格中的单词,例如 AirPod

OR,你可以把我的要求当成When a cell contains the GA, the text of that cell's label changes to "Touch the Green"

我的问题是,当我滚动 tableView 时,我怎样才能得到 which one(Cell) 包含 GA? 但我找不到获取该信息的方法(关于 That Cell 的一些位置/索引信息

谁能帮帮我? ObjectiveC 解决方案还可以,Swift 解决方案更适合我,非常感谢

【问题讨论】:

  • 你能分享截图吗?你需要什么精确的屏幕?
  • Bcos,你不能将 UITableView 用作 UIPickerView。所以可能的共享屏幕。
  • stackoverflow.com/questions/31515151/… 此链接中有一张图片。我根据这张图问问题,这张图是否把问题说清楚了?
  • 我理解你的逻辑。 UIPickerView 将节省您的时间。但是,如果你在 UITableView 上需要这个逻辑意味着,屏幕需要。
  • the image in my prototype 我想我必须使用 UITableView(服务器中有很多数据)。我的要求是当单元格包含固定在屏幕中的绿色区域时,音频播放器会发出声音。

标签: ios uitableview uiscrollview


【解决方案1】:

在这段代码中,我在UIView 的中心使用GreenArea。来自Ruslan's答案的一些修改。

@IBOutlet weak var greenAreaVw: UIView!
var contHeight : CGFloat = 0.0
var eachRowHeight : CGFloat = 45
var topSpaceTableView : CGFloat = 62
var GreenAreaOriginY : CGFloat = 0.0

// Give UITableView Edge Insets in ViewDidLoad

contHeight = ((self.view.frame.size.height / 2) - eachRowHeight / 2 - topSpaceTableView)
userTblVw.contentInset = UIEdgeInsets(top: contHeight, left: 0, bottom: contHeight, right: 0)
userTblVw.contentOffset.y = -contHeight
GreenAreaOriginY = greenAreaVw.frame.origin.y

/*-------------------         -----------------------*/
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

     checkCells()  
}

func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {

     checkCells()  
}


func checkCells() {

    userTblVw.visibleCells.forEach { cell in
        if let indexPath = userTblVw.indexPathForCell(cell) {

            let rect = userTblVw.rectForRowAtIndexPath(indexPath)
            let convertedRect = self.userTblVw.convertRect(rect, toView: self.view)

            if convertedRect.origin.y >= GreenAreaOriginY && convertedRect.origin.y < (GreenAreaOriginY + eachRowHeight)   
            {
                let contFloat : CGFloat = (eachRowHeight * CGFloat(indexPath.row)) - contHeight
                userTblVw.setContentOffset(CGPoint(x: 0, y: contFloat), animated: true)
            }

        }
    }
}

在下面找到截图:

【讨论】:

  • 非常感谢您的帮助,麦当劳,您的解决方案真是太好了,使tableView具有像pickerView一样的效果,比我的要求更强大。
  • 没有足够的声望。当我得到足够的,我会支持你的答案
【解决方案2】:
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    // We check cells here to set the state of whether it contains the green or not before the scrolling
    checkCells()
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    // And we are continuously checking cells while scrolling
    checkCells()
}

func checkCells() {
    tableView.visibleCells.forEach { cell in
        if let indexPath = tableView.indexPath(for: cell) {
            let rect = tableView.rectForRow(at: indexPath)
            // This is the rect in your VC's coordinate system (and not the table view's one)
            let convertedRect = self.view.convert(rect, from: tableView)

            if convertedRect.contains(greenArea.frame) {
                cell.textLabel?.text = "Touch the Green"
            } else {
                cell.textLabel?.text = "Does not touch the Green"
            }
        }
    }
}

【讨论】:

  • 非常感谢您的帮助,鲁斯兰。你的代码清晰易懂,正是我想要的。
【解决方案3】:

怎么样:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    [0, 1, 2].forEach {
        let rect = tableView.rectForRow(at: IndexPath(row: $0, section: 0))
        if rect.contain(GAView.frame) {
            // play sound here
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多