简体   繁体   English

使用Swift 3在tableVIew中进行单元格自动选择

[英]Cell automatic selection in tableVIew using swift 3

I have a table with about 40 records. 我有一张约有40条记录的表。 I want to always select the cell that is in the center of the screen, in a certain position. 我想始终选择位于屏幕中央某个位置的单元格。 Whenever I do the scrool from the table, it should change the selected row to the one that is currently in the center. 每当我从表中进行scrool时,它都应将所选行更改为当前位于中心的行。 It would be the same as a picker but the row has several images and labels so it will not be possible to make a Picker. 它将与选取器相同,但是该行具有多个图像和标签,因此将无法制作选取器。

Is it possible to do something similar? 是否可以做类似的事情?

This is possible with a UIPickerView, and it does sound like that's what you're trying to recreate. 使用UIPickerView可以做到这一点 ,而且听起来确实是您要重新创建的东西。

All you need to do is replace the view in the UIPickerView. 您需要做的就是替换UIPickerView中的视图。 There are two methods you need which aren't normally required - you need to specify the height, and you need to define the view to be used for the row 通常不需要使用两种方法-需要指定高度,并且需要定义要用于行的视图

func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat 
{
    return self.myRowHeight
}

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView 
{
    var myView = UIView(frame: CGRectMake(0, 0, pickerView.bounds.width, self.myRowHeight))
    // define & add whatever controls you need   
    myView.addSubview(myControl1)
    myView.addSubview(myControl2)

    return myView
}

Yes it's possible, but it sounds like a bad user interface. 是的,这是可能的,但听起来像是一个不良的用户界面。

UITableView is a subclass of UIScrollview. UITableView是UIScrollview的子类。 You could set yourself up as the scroll view delegate, and in response to delegate calls like scrollViewDidScroll(_:) and/or scrollViewDidEndDecelerating(_:) , change the selection. 您可以将自己设置为滚动视图委托,并响应诸如scrollViewDidScroll(_:)和/或scrollViewDidEndDecelerating(_:)类的委托调用,更改选择。

However I think you'd have a better user experience if you used a picker view. 但是,我认为如果使用选择器视图,将会有更好的用户体验。 You can set up picker views to use a view for each index, so you should be able to serve up a custom view that contains multiple image views and labels as subviews. 您可以设置选择器视图以对每个索引使用一个视图,因此您应该能够提供一个包含多个图像视图和标签作为子视图的自定义视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM