Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 用于2个表视图/分段控件的自定义单元格_Ios_Swift_Uitableview_Uisegmentedcontrol_Tableviewcell - Fatal编程技术网

Ios 用于2个表视图/分段控件的自定义单元格

Ios 用于2个表视图/分段控件的自定义单元格,ios,swift,uitableview,uisegmentedcontrol,tableviewcell,Ios,Swift,Uitableview,Uisegmentedcontrol,Tableviewcell,因此,我有一个分段控件,在MainVC中的2个TableView和1个MapView之间切换 我可以通过添加IBAction func changedInSegmentedControl来切换模拟器中的视图,以切换其中一个视图处于活动状态时隐藏的视图 我用XIB创建了2个自定义TableViewCell。我还为每个TableView添加了标记 我的问题是如何将它们添加到cellForRowAtIndexPath中 目前,我的代码是: func tableView(tableView: UITab

因此,我有一个分段控件,在MainVC中的2个TableView和1个MapView之间切换

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

我用XIB创建了2个自定义TableViewCell。我还为每个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单元格:UITableViewCell,但在完成dequeuReusableCellWithIdentifier时遇到了问题


有人知道怎么做吗?谢谢。

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

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
}

我就是这样做的(iOS 10上的Swift 3.0)。我制作了一个带有两个自定义单元格的tableView(每个单元格都是它们自己的子类)。分段控件位于我的导航栏上,有两个分段:人和地点

在我的类中有两个数组(people和places),它们是两个表视图的数据源。附加到segmentedControl的操作触发表的重新加载,而
CellForRowatinex
中的switch语句控制从哪个单元格加载数组

我通过一个API调用将数据加载到两个数据数组中,异步完成会触发
dataload()
,从而重新加载tableView。同样,我不必担心在重新加载表时选择哪个段:
cellforrowatinex
负责加载正确的数据。 我初始化一个基本单元格,就像初始化UITableViewCell一样,然后在case语句中创建并配置自定义单元格。然后我在最后返回我的自定义类型单元格,只要重用标识符和类在
cellforrowatinex
中正确,正确的单元格就会初始化并显示在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
}
@ibvar-tableView:UITableView!
@IBOutlet弱var peoplePlacesControl:UISegmentedControl!
fileprivate变量placesArray:[PlaceModel]?
fileprivate var usersArray:[用户模型]?
@iAction func segmentedControlActionChanged(\uSender:AnyObject){
tableView.reloadData()
开关分段控制。选择分段索引{
案例0:
loadUsersfromAPI()
案例1:
loadPlacesFromAPI()
违约:
//不应该到这里
返回
}
}
func dataLoaded(){
切换人物位置控件。已选择分段索引{
案例0://用户
如果favoriteUsersArray==nil{
self.tableView.reloadData()
}否则{
hideTableViewWhileEmpty()
}
案例1://地点
如果placesArray!=零{
self.tableView.reloadData()
}否则{
hideTableViewWhileEmpty()
}
违约:
返回
}
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
开关分段控制。选择分段索引{
案例0:
如果usersArray!=nil{
返回usersArray!.count
}否则{
返回0
}
案例1://地点
如果placesArray!=零{
返回位置Ray!。计数
}否则{
返回0
}
违约:
返回0
}
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
var cell=UITableViewCell()
切换人物位置控件。已选择分段索引{
案例0://人
让userCell=tableView.dequeueReusableCell(标识符为:“MyUserCell”,用于:indexath)作为!MyUserTableViewCell
如果usersArray!=nil&&indepath.row
我以前试过。获取编译器错误“类型'UITableViewCell'不符合协议NilliterAverable。Cell如何在情节提要中注册nib或原型单元格?”?我在tableview中注册了两个不同的单元格。@27J91,如果您使用的是故事板,您可以将重用标识符字符串放入每个原型单元格的界面生成器字段
reuseIdentifier
。这就是我在示例中使用故事板时所做的。或者,您也可以通过编程方式注册原型单元格,方法是(在
viewDidLoad
中)
tableView.registerClass(cellClass:MyUserTableViewCell,forCellWithReuseIdentifier标识符:“UserCellReuseIdentifier”)
。只要为每个单元格注册一个reuseIdentifier,UITableView就可以在需要时将两种不同类型的可重用单元格出列。