用于2个TableViews /分段控制Swift的自定义单元格

时间:2015-07-27 15:30:23

标签: ios swift uitableview uisegmentedcontrol tableviewcell

所以我有一个分段控件,可以在2个TableViews& 1 MainVC中的MapView。

我可以通过添加IBAction func changedInSegmentedControl来切换模拟器中的视图,以切换哪些视图在其中一个处于活动状态时被隐藏。

我用XIB创建了2个自定义TableViewCells。我还为每个TableView添加了标签。

我的问题是如何在cellForRowAtIndexPath中添加它们?

目前,我的代码是:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
  {
    // var cell: UITableViewCell
    if (tableView.tag == 1) {
      cell: CardTableViewCell = tableView.dequeueReusableCellWithIdentifier("CardCell") as! CardTableViewCell
return cell
      }
    else if (tableView.tag == 2) {
      cell: ListTableViewCell = tableView.dequeueReusableCellWithIdentifier("ListCell") as! ListTableViewCell
return cell
    }
  }

当然,Swift需要一个"返回单元"对于If语句之外的函数。我尝试使用var cell:UITableViewCell外部,但在完成dequeuReusableCellWithIdentifier时遇到了麻烦。

任何人都知道如何做到这一点?感谢。

2 个答案:

答案 0 :(得分:0)

我已对您的代码进行了更改。 使用以下代码

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    if (tableView.tag == 1) {

        cell: CardTableViewCell = tableView.dequeueReusableCellWithIdentifier("CardCell") as! CardTableViewCell
        return cell
    }

    cell: ListTableViewCell = tableView.dequeueReusableCellWithIdentifier("ListCell") as! ListTableViewCell
    return cell
}

答案 1 :(得分:0)

这就是我接近它的方式(iOS 10上的Swift 3.0)。我用两个自定义单元格创建了一个tableView(每个单元格都是自己的子类)。分段控件位于我的navigationBar上,有两个部分:People和Places。

我的类中有两个数组(人和地点),它们是两个表视图的数据源。附加到segmentedControl的操作会触发表的重新加载,cellForRowAtIndex中的switch语句控制从哪个单元加载数组。

我从API调用将数据加载到两个数据数组中,异步完成触发dataLoaded(),重新加载tableView。同样,我不必担心重新加载表时选择了哪个段:cellForRowAtIndex负责加载正确的数据。 我像UITableViewCell一样初始化一个基本单元格,然后在我创建的case语句中并配置自定义单元格。然后我在最后返回我的自定义类型单元格,只要在cellForRowAtIndex中的reuseIdentifiers和类正确,就会初始化正确的单元格并显示在tableView中。

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var peoplePlacesControl: UISegmentedControl!

fileprivate var placesArray: [PlaceModel]?
fileprivate var usersArray: [UserModel]?


@IBAction func segmentedControlActionChanged(_ sender: AnyObject) {
    tableView.reloadData()
    switch segmentedControl.selectedSegmentIndex {
    case 0: 
        loadUsersfromAPI()
    case 1: 
        loadPlacesFromAPI()
    default:
        // shouldnt get here
        return
    }
}


func dataLoaded() {
    switch peoplePlacesControl.selectedSegmentIndex {
    case 0: // users
        if favoriteUsersArray == nil {
            self.tableView.reloadData()
        } else {
            hideTableViewWhileEmpty()
        }
    case 1: // places
        if placesArray != nil {
            self.tableView.reloadData()
        } else {
            hideTableViewWhileEmpty()
        }
    default: 
        return
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    switch segmentedControl.selectedSegmentIndex {
    case 0:  
        if usersArray != nil {
            return usersArray!.count
        } else {
            return 0 
        }
    case 1:  // places
        if placesArray != nil {
            return placesArray!.count
        } else {
            return 0  
        }
    default:
        return 0
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    var cell = UITableViewCell()
    switch peoplePlacesControl.selectedSegmentIndex {
    case 0:  // people
        let userCell = tableView.dequeueReusableCell(withIdentifier: "MyUserCell", for: indexPath) as! MyUserTableViewCell
        if usersArray != nil && indexPath.row < usersArray!.count {
            let user = usersArray![indexPath.row]
            userCell.configure(user)
            userCell.myDelegate = self
        }
        cell = userCell as MyUserTableViewCell

    case 1:  // places
        let placeCell = tableView.dequeueReusableCell(withIdentifier: "MyPlaceCell", for: indexPath) as! MyPlaceTableViewCell

        if favoritePlacesArray != nil && indexPath.row < favoritePlacesArray!.count {
            let place = placesArray![indexPath.row]
            placeCell.configure(place)
            placeCell.myDelegate = self
        }
        cell = placeCell as MyPlaceTableViewCell

    default:
        break
    }
    return cell
}
相关问题