2014-06-17 27 views
14
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 
    @IBOutlet 
    var tableView: UITableView 
    var items: String[] = ["We", "Heart", "Swift"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "myCell") 
    } 


    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int { 
     return self.items.count; 
    } 

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

     var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("myCell") as UITableViewCell 

     cell.textLabel.text = self.items[indexPath.row] 
     cell.accessoryType = UITableViewCellAccessoryType.DetailDisclosureButton 
     cell.selectionStyle = UITableViewCellSelectionStyle.Blue 
     tableView.separatorStyle = UITableViewCellSeparatorStyle.None 
     return cell 
    } 

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { 
     println("You selected cell #\(indexPath.row)!") 

    } 
} 

我的問題是,accessoryType和selectionStyle沒有得到改變。 tableView.separatorStyle確實和cell.textlabel.text一樣發生了變化。 我該如何解決這個問題?當被選擇時斯威夫特的tableView細胞集附件類型

+0

注意:'tableView:cellForRowAtIndexPath:'可能不是設置'tableView.separatorStyle'的正確位置。我會將這行代碼移到'viewWillAppear',或者甚至嘗試在界面生成器中進行設置(我沒有訪問最新的Xcode,所以我不能快速嘗試)。 – dasblinkenlight

回答

21

UITableViewCellSelectionStyleBlue

該電池具有一個默認的背景顏色。

在iOS 7中,選擇顏色不再是藍色。改爲使用 UITableViewCellSelectionStyleDefault。

至於accessoryType,只要你以後不在其他地方改變它,它應該可以正常工作。確保桌子寬度正確,否則附件視圖可能會脫離屏幕。

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    @IBOutlet 
    var tableView: UITableView 

    var items: String[] = ["We", "Heart", "Swift"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "myCell") 
    } 


    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int { 
     return self.items.count; 
    } 

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

     var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("myCell") as UITableViewCell 
     cell.textLabel.text = self.items[indexPath.row] 
     cell.selectionStyle = UITableViewCellSelectionStyle.Blue 

     /* 
     enum UITableViewCellAccessoryType : Int { 
     case None // don't show any accessory view 
     case DisclosureIndicator // regular chevron. doesn't track 
     case DetailDisclosureButton // info button w/ chevron. tracks 
     case Checkmark // checkmark. doesn't track 
     case DetailButton // info button. tracks 
     } 
     */ 

     // Standard options 
     cell.accessoryType = UITableViewCellAccessoryType.None 
     cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator 
     cell.accessoryType = UITableViewCellAccessoryType.DetailDisclosureButton 
     cell.accessoryType = UITableViewCellAccessoryType.Checkmark 
     cell.accessoryType = UITableViewCellAccessoryType.DetailButton 

     // Custom view options 
     cell.accessoryType = UITableViewCellAccessoryType.None 
     cell.accessoryView = UIView(frame: CGRectMake(0, 0, 20, 20)) 
     cell.accessoryView.backgroundColor = UIColor.blueColor() 

     return cell 
    } 

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { 
     println("You selected cell #\(indexPath.row)!") 

    } 
} 

請注意,這不是一個很好的解決方案,以每次請求電池時間設置表的separatorStyle,而不是當tableView加載做一次:在viewDidLoad

+0

我想更改附件類型。 我該如何改變它? (以及在哪裏?) – user3748930

+0

您可以在'UITableViewCellAccessoryType'枚舉中選擇任何可用選項。如果你想要一個定製的配件視圖,廚師出UITableViewCell屬性'accessoryView'。 –

+0

我必須爲單元格創建一個類嗎? – user3748930

1

我想和大家分享我的這段經歷,我有同樣的問題,cell.accessoryType = IUTableViewCellAccessoryType.Checkmark 而且我注意到我的tableview沒有限制,所以我加了缺少約束,然後它的工作對我來說

6

我沒有」沒有任何運氣在cellForRowAtIndexPath方法中設置它,將它移動到willDisplayCell解決了它沒有出現的問題。

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    cell.accessoryType = .DisclosureIndicator 
}