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 将多个原型单元放入UITableView中_Ios_Swift_Uitableview_Viewcontroller - Fatal编程技术网

Ios 将多个原型单元放入UITableView中

Ios 将多个原型单元放入UITableView中,ios,swift,uitableview,viewcontroller,Ios,Swift,Uitableview,Viewcontroller,我想要一个具有多个原型单元的TableView。我在网上找到了一些解决方案,但它们有太多重复代码,尤其是在我的系统扩展时。如何编写一个工作方式类似但定义了switch或条件语句之外的单元格的解决方案 这是解决方案中的工作代码: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { cell.userInteractionEnabled

我想要一个具有多个原型单元的TableView。我在网上找到了一些解决方案,但它们有太多重复代码,尤其是在我的系统扩展时。如何编写一个工作方式类似但定义了switch或条件语句之外的单元格的解决方案

这是解决方案中的工作代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
cell.userInteractionEnabled = false
if indexPath.section == 0 {
    let cell: FlightsDetailCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as FlightsDetailCell
    cell.graphView.enableBezierCurve = true
    cell.graphView.enableReferenceYAxisLines = true
    cell.graphView.enableYAxisLabel = true
    cell.graphView.colorYaxisLabel = UIColor.whiteColor()
    cell.graphView.delegate = UIApplication.sharedApplication().delegate as BEMSimpleLineGraphDelegate
    cell.graphView.dataSource = UIApplication.sharedApplication().delegate as BEMSimpleLineGraphDataSource
    return cell
}

else if indexPath.section == 1 {
    let cell: FlightsDetailCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as FlightsDetailCell
    cell.graphView.enableBezierCurve = true
    cell.graphView.enableReferenceYAxisLines = true
    cell.graphView.enableYAxisLabel = true
    cell.graphView.colorYaxisLabel = UIColor.whiteColor()
    cell.graphView.delegate = self
    cell.graphView.dataSource = self
    return cell
}    

else {
    let cell2: FlightsInformationCell = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as FlightsInformationCell
    cell2.userInteractionEnabled = false
    return cell2
}
我想要一个看起来像

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! UITableViewCell
    switch (variable){
        case "1":
            cellIdentifier = "Identifier"
            cell = cell as! MyTableViewCell
        case "2":
            cellIdentifier = "anotherIdentifier"
            cell = cell as! MyOtherTableViewCell
    }
    cell.a.text = "hello"
    cell.b.text = "hello"
    ...
    return cell
}

因为在
开关
的情况下不能完全使用上述代码,因为并没有为特定单元格定义所有通用的东西。但是,是的,您可以通过以下方式简单地修改代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

var cell: AnyObject?

switch(indexPath.section){

    case 0:

        cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! FlightsDetailCell
        cell.userInteractionEnabled = false
        cell.graphView.enableBezierCurve = true
        cell.graphView.enableReferenceYAxisLines = true
        cell.graphView.enableYAxisLabel = true
        cell.graphView.colorYaxisLabel = UIColor.whiteColor()
        cell.graphView.delegate = UIApplication.sharedApplication().delegate as BEMSimpleLineGraphDelegate
        cell.graphView.dataSource = UIApplication.sharedApplication().delegate as BEMSimpleLineGraphDataSource    

    case 1:

        cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! FlightsDetailCell
        cell.userInteractionEnabled = false
        cell.graphView.enableBezierCurve = true
        cell.graphView.enableReferenceYAxisLines = true
        cell.graphView.enableYAxisLabel = true
        cell.graphView.colorYaxisLabel = UIColor.whiteColor()
        cell.graphView.delegate = self
        cell.graphView.dataSource = self

    default:
        cell = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as! FlightsInformationCell
        ce.userInteractionEnabled = false
    }

    return cell
}

希望这有帮助

因为您不可能在
开关
的情况下完全使用上述代码,因为并没有为特定单元格定义所有通用的东西。但是,是的,您可以通过以下方式简单地修改代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

var cell: AnyObject?

switch(indexPath.section){

    case 0:

        cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! FlightsDetailCell
        cell.userInteractionEnabled = false
        cell.graphView.enableBezierCurve = true
        cell.graphView.enableReferenceYAxisLines = true
        cell.graphView.enableYAxisLabel = true
        cell.graphView.colorYaxisLabel = UIColor.whiteColor()
        cell.graphView.delegate = UIApplication.sharedApplication().delegate as BEMSimpleLineGraphDelegate
        cell.graphView.dataSource = UIApplication.sharedApplication().delegate as BEMSimpleLineGraphDataSource    

    case 1:

        cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! FlightsDetailCell
        cell.userInteractionEnabled = false
        cell.graphView.enableBezierCurve = true
        cell.graphView.enableReferenceYAxisLines = true
        cell.graphView.enableYAxisLabel = true
        cell.graphView.colorYaxisLabel = UIColor.whiteColor()
        cell.graphView.delegate = self
        cell.graphView.dataSource = self

    default:
        cell = tableView.dequeueReusableCellWithIdentifier("Cell2", forIndexPath: indexPath) as! FlightsInformationCell
        ce.userInteractionEnabled = false
    }

    return cell
}
希望这有帮助