2016-07-24 59 views
0

我在一個視圖控制器中創建了多個UITableViews,並且我使用下面的代碼來檢查它是哪個tableView。我一直在問題「在最後一行中使用未解析的標識符」單元格。我試過尋找這個,但是在Xcode 7中並沒有太多最新的東西。請原諒我缺乏術語以及的經驗,我剛開始起飛了,請隨時問我的,你可能遇到的任何問題,謝謝大家提前誰花時間閱讀這篇文章的時候!使用未解析的標識符「單元格」

@IBOutlet var tableView1: UITableView! 
@IBOutlet var tableView2: UITableView! 

// Mens games Variables 
var dates = ["7/23", "7/24","7/25","7/26","7/27","7/28","7/29"] 
var times = ["7:30","8:00","8:30","9:00","9:30","10:00","10:30"] 
var teamsOne = ["VU Reserves 1","VU Reserves 3","VU Reserves 5","VU Reserves 7","VU Reserves 9","VU Reserves 11","VU Reserves 13"] 
var teamsTwo = ["VU Reserves 2","VU Reserves 4","VU Reserves 6","VU Reserves 8","VU Reserves 10","VU Reserves 12","VU Reserves 14"] 
var fields = ["Turf","Field 1","Field 2","Field 3","Field 4","Field 5","Field 6"] 


// Womens games Variables 
var womensDates = ["7/23", "7/24","7/25","7/26","7/27","7/28","7/29"] 
var womensTimes = ["7:30","8:00","8:30","9:00","9:30","10:00","10:30"] 
var womensTeamsOne = ["VU Girls 1","VU Girls 3","VU Girls 5","VU Girls 7","VU Girls 9","VU Girls 11","VU Girls 13"] 
var womensTeamsTwo = ["VU Girls 2","VU Girls 4","VU Girls 6","VU Girls 8","VU Girls 10","VU Girls 12","VU Girls 14"] 
var womensFields = ["Turf","Field 1","Field 2","Field 3","Field 4","Field 5","Field 6"] 




override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView1.dataSource = self 
    tableView1.delegate = self 
    tableView1.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell1") 
    tableView2.dataSource = self 
    tableView2.delegate = self 
    tableView2.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell2") 
} 


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


    if (tableView == tableView1) { 
     return teamsOne.count 
    } 
    else if (tableView == tableView2) { 

    return womensTeamsOne.count 

    } else{ 

     return 0 } 

    } 

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


    if (tableView == tableView1) { 

    let cell = self.tableView1.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! gamesCustomCell 

    cell.date.text = dates[indexPath.row] 
    cell.time.text = times[indexPath.row] 
    cell.teamOne.text = teamsOne[indexPath.row] 
    cell.teamTwo.text = teamsTwo[indexPath.row] 
    cell.field.text = fields[indexPath.row] 

     return cell 

    } else if (tableView == tableView2) { 


     let cell = self.tableView2.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as! womensGamesCustomCell 

     cell.womensDate.text = womensDates[indexPath.row] 
     cell.womensTime.text = womensTimes[indexPath.row] 
     cell.womensTeamOne.text = womensTeamsOne[indexPath.row] 
     cell.womensTeamTwo.text = womensTeamsTwo[indexPath.row] 

    return cell 

    } 

    return cell 
    } 
    } 
+0

這是因爲上次返回單元格沒有聲明。 – Chandan

+0

我照你告訴我的那樣做了,但是我現在得到錯誤,說「常量」單元格在被初始化之前使用了「 –

+0

我已經更新了答案請現在嘗試 – Chandan

回答

1

只需用下面的代碼

取代的cellForRowAtIndexPath
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
var cell:UITableViewCell? 

if (tableView == tableView1) { 

cell = self.tableView1.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! gamesCustomCell 

cell.date.text = dates[indexPath.row] 
cell.time.text = times[indexPath.row] 
cell.teamOne.text = teamsOne[indexPath.row] 
cell.teamTwo.text = teamsTwo[indexPath.row] 
cell.field.text = fields[indexPath.row] 

} 
if (tableView == tableView2) { 

    cell = self.tableView2.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as! womensGamesCustomCell 

    cell.womensDate.text = womensDates[indexPath.row] 
    cell.womensTime.text = womensTimes[indexPath.row] 
    cell.womensTeamOne.text = womensTeamsOne[indexPath.row] 
    cell.womensTeamTwo.text = womensTeamsTwo[indexPath.row] 

} 

return cell! 
} 
+0

如果您在最後強制解包它,單元不需要ti是一個可選項。 – penatheboss

0

試試這個,它爲我工作

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if (tableView == tableView1) { 
    let cell = self.tableView1.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! gamesCustomCell 

    cell.date.text = dates[indexPath.row] 
    cell.time.text = times[indexPath.row] 
    cell.teamOne.text = teamsOne[indexPath.row] 
    cell.teamTwo.text = teamsTwo[indexPath.row] 
    cell.field.text = fields[indexPath.row] 

    return cell 

    } else if (tableView == tableView2) { 
    let cell = self.tableView2.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as! womensGamesCustomCell 

    cell.womensDate.text = womensDates[indexPath.row] 
    cell.womensTime.text = womensTimes[indexPath.row] 
    cell.womensTeamOne.text = womensTeamsOne[indexPath.row] 
    cell.womensTeamTwo.text = womensTeamsTwo[indexPath.row] 

    return cell 
    } 

    return UITableViewCell() 
}