ios - 如何在1个ViewController中实现2个UITableView?每个 tableView 都有自己的自定义单元格

标签 ios swift uitableview

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

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

最佳答案

下面是如何在一个 viewController 中配置两个 tableView(这可以使用 2 个或更多 tableView)。

 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

    }

}

关于ios - 如何在1个ViewController中实现2个UITableView?每个 tableView 都有自己的自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36778630/

相关文章:

启动屏幕后 iOS 应用程序崩溃

ios - 我将如何实现基于堆叠卡片的 View ?

ios - tableview 滚动得到错误的最后一个单元格

ios - 在 UITableViewCell 中复制 UITextField

ios - 在 UITableViewCell 中添加 UICollectionView 时出错

ios - 添加 UItextField 输入并将其保存到数组

ios - 我如何从 JSON 文件创建 Core Data managedObjects 并让它们在应用程序启动之间保持不变?

ios - iOS 应用上 Web Trends SDK 的谷歌移动广告问题

ios - UIView setImage :]: unrecognized selector sent to instance

ios - 向 UITableView 添加新项目而不替换旧项目