我试图从自定义段控制单元管理到ViewController的表视图单元。
这是我的自定义分段控制类:
protocol SegmentControllerCellDelegate: AnyObject {
func manageSegmentControl(cell: SegmentControllerCell)}
class SegmentControllerCell: UITableViewCell, ReusableView, NibLoadableView {
@IBOutlet weak var segmentController: UISegmentedControl!
weak var delegate: SegmentControllerCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
setupUI()
}
@IBAction func tappedSegmentControll(_ sender: UISegmentedControl) {
self.delegate?.manageSegmentControl(cell: self )
}}
这是我的视图控制器:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch BrandSection(rawValue: indexPath.section)! {
case .profile:
guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: BrandTableViewCell.self), for: indexPath) as? BrandTableViewCell else { return UITableViewCell() }
cell.cellType = .brandPage
return cell
case .segment:
guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: SegmentControllerCell.self), for: indexPath) as? SegmentControllerCell else { return UITableViewCell() }
cell.delegate = self
return cell
case .products:
guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: BrandProductionsCell.self), for: indexPath) as? BrandProductionsCell else { return UITableViewCell() }
return cell
}
}
extension BrandProfileVC: SegmentControllerCellDelegate {
func manageSegmentControl(cell: SegmentControllerCell) {
if cell.segmentController.selectedSegmentIndex == 0 {
self.brandTableView.reloadData()
print("index 0")
} else if cell.segmentController.selectedSegmentIndex == 1 {
self.brandTableView.reloadData()
print("index 1")
}
}}
我只想显示当我点击分段控制选项卡时,分段控制下的不同单元格。
请尝试从cellForRow
将cell.tag
设置为indexPath.row
,然后尝试在ViewController 中打印manageSegmentControl
内的cell.tag
这是代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch BrandSection(rawValue: indexPath.section)! {
case .profile:
guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: BrandTableViewCell.self), for: indexPath) as? BrandTableViewCell else { return UITableViewCell() }
cell.cellType = .brandPage
cell.tag = indexPath.row
return cell
case .segment:
guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: SegmentControllerCell.self), for: indexPath) as? SegmentControllerCell else { return UITableViewCell() }
cell.delegate = self
cell.tag = indexPath.row
return cell
case .products:
guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: BrandProductionsCell.self), for: indexPath) as? BrandProductionsCell else { return UITableViewCell() }
cell.tag = indexPath.row
return cell
}
}
extension BrandProfileVC: SegmentControllerCellDelegate {
func manageSegmentControl(cell: SegmentControllerCell) {
if cell.segmentController.selectedSegmentIndex == 0 {
self.brandTableView.reloadData()
print(cell.tag)
} else if cell.segmentController.selectedSegmentIndex == 1 {
self.brandTableView.reloadData()
print(cell.tag)
}
}
}