简体   繁体   中英

Cell automatic selection in tableVIew using swift 3

I have a table with about 40 records. 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. 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.

All you need to do is replace the view in the 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. You could set yourself up as the scroll view delegate, and in response to delegate calls like scrollViewDidScroll(_:) and/or scrollViewDidEndDecelerating(_:) , change the selection.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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