我有2个tableview,每个都有自己的自定义单元格。如何在1个viewcontroller中实现这些表视图?这是我的代码。
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个或更多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
}
}
关于ios - 如何在1个ViewController中实现2个UITableViews?每个tableViews都有自己的自定义单元格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36778630/