2016-05-10 48 views
2

我有錯誤:當我嘗試改變的tableView的背景在viewWillAppear方法值類型「的UIView」的沒有成員「的tableView」

Value of type 'UIView' has no member 'tableView'

發生。

我預計表格視圖已經從viewDidLoad方法初始化。

我應該在哪裏嘗試改變tableview的顏色?我是否也應該在同一個地方改變其他背景顏色?

class settingsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    override func viewDidLoad() { 
    super.viewDidLoad() 

    // Table view setup 
    let tableView = UITableView(frame: view.bounds, style: .Plain) 
    tableView.delegate = self 
    tableView.dataSource = self 
    tableView.registerClass(settingsCell.self, forCellReuseIdentifier: NSStringFromClass(settingsCell)) 
    view.addSubview(tableView) 

    } 

    override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(true) 

    if nightMode == true { 
     view.backgroundColor = UIColor.blackColor() 
     view.tableView.backgroundColor = UIColor.blackColor() // Error here.. 
    } 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return settings.count 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier(NSStringFromClass(settingsCell), forIndexPath: indexPath) as! settingsCell 

    // Fetch setting 
    let cellSetting = settings[indexPath.row] 

    // Configure Cell 
    cell.textLabel?.text = cellSetting.name 

    return cell 
    } 
} 
+0

有什麼目的,不要使用'UITableViewController'或至少一個實例變量? – vadian

+0

你有你的tableView本地作用域。把它放在viewDidLoad之外的頂部。就目前而言,只有viewDidLoad「看到」它。請參閱Bhavin的答案 – Adrian

+0

我主要是在做這個練習,在UIViewController中我需要手動做更多的事情。 http://stackoverflow.com/questions/14465447/are-there-any-advantages-to-using-a-uitableviewcontroller-over-a-uiviewcontrolle –

回答

2

你可以把它的實例變量UITableView like

class settingsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

var tableView:UITableView! 
override func viewDidLoad() { 
     super.viewDidLoad() 

     // Table view setup 
     tableView = UITableView(frame: view.bounds, style: .Plain) 
     tableView.delegate = self 
     tableView.dataSource = self 
     tableView.registerClass(settingsCell.self, forCellReuseIdentifier: NSStringFromClass(settingsCell)) 
     view.addSubview(tableView) 
} 

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(true) 

    if nightMode == true { 
     view.backgroundColor = UIColor.blackColor() 
     tableView.backgroundColor = UIColor.blackColor() 
    } 
} 
+0

1)這並沒有定義'tableView'「全局」(具有不同的含義)。這使它成爲一個實例變量。 2)當你聲明實例變量時,不需要創建臨時的'UITableView'實例。 – rmaddy

+0

我在Swift上很不流利,但不應該'tableView'實例變量的聲明只是'var tableView:UITableView'?爲什麼要添加'= UITableView()'部分? – rmaddy

+1

'= UITableView()'創建一個新的表格視圖。但是,你在'viewDidLoad'中創建另一個。創建初始表格視圖沒有意義。 – rmaddy

1

這不是訪問視圖中子視圖的正確方法。您可以使用viewWithTag:通過標籤來獲取子視圖:

override func viewDidLoad() { 
    ... 
    tableView.tag = 100 
    view.addSubview(tableView) 
} 

... 

override func viewWillAppear(animated: Bool) { 
    ... 
    if let tableView = view.viewWithTag(100) { 
    tableView.backgroundColor = UIColor.blackColor() 
    } 
} 

或者,你讓tableView一個實例變量,如果你需要訪問它不僅僅是viewWillAppear

class SettingsViewController: UIViewController ... { 
    var tableView: UITableView! 
    ... 

    override func viewDidLoad() { 
     ... 
     self.tableView = UITableView(frame: view.bounds, style: .Plain) 
     ... 
    } 

    override func viewWillAppear(animated: Bool) { 
     ... 
     self.tableView.backgroundColor = UIColor.blackColor() 
    } 
} 
+2

或者讓'tableView'成爲一個實例變量。這會好很多,因爲代碼可能需要不僅僅是'viewWillAppear'來訪問'tableView'。 – rmaddy

1

聲明tableview中的全局值,然後在類中使用tableview,s ee值下面的代碼,

 class ViewController: UIViewController { 
      var tableView = UITableView() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     tableview.frame = self.view.bounds 
     tableView.delegate = self 
     tableView.dataSource = self 
     tableView.registerClass(settingsCell.self, forCellReuseIdentifier: NSStringFromClass(settingsCell)) 
     self.view.addSubview(tableView) 
     } 

    override func viewWillAppear(animated: Bool) { 
      if nightMode == true { 
       view.backgroundColor = UIColor.blackColor() 
       tableView.backgroundColor = UIColor.blackColor() 
     } 
} 

希望它有幫助

+0

爲什麼要包含IBOutlets? –

+0

對不起,我有我的代碼副本, –

相關問題