1.tableView
//默认选中的section,row
let defaultSelectCell = IndexPath(row: 0, section: 0)
self.aTableView.selectRow(at: defaultSelectCell, animated: true, scrollPosition: UITableViewScrollPosition.none)
2.CollectionView
let defaultSelectCell = IndexPath(row: 0, section: 0)
self.aCollectionView.selectItem(at: defaultSelectCell, animated: true, scrollPosition: UICollectionViewScrollPosition.left)
3.具体使用
先将tableView初始化完毕,后调用此方法将cell移动到选择的位置,在做cell的选择时,不要使用deselectRowAt方法,在选中时刷新cell.
///初始化默认frame
convenience init(frame: CGRect,selectRow: Int){
self.init(frame: frame)
self.selectedCell = selectRow
//默认选中的cell
let defaultCell = IndexPath(row: selectedCell, section: 0)
//默认选中
self.aTableView.selectRow(at: defaultCell, animated: true, scrollPosition: UITableViewScrollPosition.top)//移动到顶端
}