如何在1个ViewController中实现2个UITableViews? tableViews每个都有自己的自定义单元格

时间:2016-04-21 19:19:21

标签: ios swift uitableview

我有2个tableviews,每个都有自己的自定义单元格。如何在1个viewcontroller中实现这些tableviews?这是我的代码。

Xcode无法将自定义单元格转换为标准UITableViewCell

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    //        let accountSize = Int(accounts.count())
    //        return accountSize;

    var count:Int?

    if tableView == self.tableView {
        count = Int(accounts.count())
    }

    if tableView == self.tableViewLoan {
        count =  Int(loans.count())
    }

    return count!
}

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

var cell:UITableViewCell?        
if tableView == self.tableView {
        //cell = tableView.dequeueReusableCellWithIdentifier("AccountsTableViewCell", forIndexPath: indexPath) as! AccountsTableViewCell
        var cell1 = tableView.dequeueReusableCellWithIdentifier("AccountsTableViewCell", forIndexPath: indexPath) as! AccountsTableViewCell

        var account:SUVTISDDA = SUVTISDDA()
        account = accounts.objectAtIndex(UInt(indexPath.row))

        var accountType = ""
        var accountNum = ""

        let accountNumber = account.Description.Value
        var accountSplit = accountNumber.componentsSeparatedByString(": ")
        if(accountSplit.count > 1){
            accountNum = accountSplit[1]
            accountType = accountSplit[0]
            print("Account number: \(accountSplit[1])")
        } else {
            accountType = account.Description.Value
            accountNum = " No Account Number"
            print("Account number unavailable")
        }

        let formatter = NSNumberFormatter()
        formatter.numberStyle = .DecimalStyle

        cell1.AccountTypeLabel!.text = accountType
        cell1.AccountNumberLabel!.text = accountNum
        cell1.AccountCurrencyLabel!.text = account.Currency.Value
        cell1.AccountAmountLabel!.text = formatter.stringFromNumber(account.AvailableBalance.Value)
        cell1.AccountTypeLabel!.textColor = UIColor(red:0, green:0.451, blue:0.682, alpha:1)

        return cell1
    }

    if tableView == self.tableViewLoan {
        var cell2 = tableView.dequeueReusableCellWithIdentifier("loanCell", forIndexPath: indexPath) as! LoanCell

        var loan: SUVTILN = SUVTILN();
        loan = loans.objectAtIndex(UInt(indexPath.row));

        cell2.descriptionLabel.text = loan.Description.Value;
        cell2.currencyLabel.text = loan.Currency.Value;
        cell2.[![valueLabel][1]][1].text = loan.LoanAmount.Value.stringValue;

        cell = cell2;
    }
    return cell;
}

http://i.stack.imgur.com/An6G2.png

2 个答案:

答案 0 :(得分:1)

Here is how you would configure two tableViews in one viewController (this can work with 2 or more tableViews).

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

    if tableView == yourFirstTableView {

        return yourFirstArray.count

    } else {

        return yourSecondArray.count

    }



}


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

    if tableView == yourFirstTableView {

        let cell1 = tableView.dequeueReusableCellWithIdentifier("yourFirstCellReuseIdentifier", forIndexPath: indexPath) as! YourFirstCustomTableViewCell

        // Configure the cell for your first tableView


        return cell1


    } else {

        let cell2 = tableView.dequeueReusableCellWithIdentifier("yourSecondCellReuseIdentifier", forIndexPath: indexPath) as! YourSecondCustomTableViewCell

        // Configure the cell for your second tableView

        return cell2


    }

}



func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    if tableView == studentsTableView {

        // Perfrom action for your first tableView

    } else {

        // Perfrom action for your second tableView

    }

}

答案 1 :(得分:0)

我不确定你要做什么。也许尝试使用ContainerViewController并将两个TableViewControllers嵌入其中。这应该会产生在一个ViewController中有两个TableView的效果,但每个TableViewController都有不同的文件。