2015-04-18 42 views
0
import UIKit 

class RestaurantTableViewController: UITableViewController, UITableViewDelegate { 

var restaurantIsVisited = [Bool](count: 21, repeatedValue: false) 

var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "Cafe Loisl", "Petite Oyster", "For Kee Restaurant", "Po's Atelier", "Bourke Street Bakery", "Haigh's Chocolate", "Palomino Espresso", "Upstate", "Traif", "Graham Avenue Meats", "Waffle & Wolf", "Five Leaves", "Cafe Lore", "Confessional", "Barrafina", "Donostia", "Royal Oak", "Thai Cafe"] 

var restaurantImages = ["cafedeadend.jpg", "homei.jpg", "teakha.jpg", "cafeloisl.jpg", "petiteoyster.jpg", "forkeerestaurant.jpg", "posatelier.jpg", "bourkestreetbakery.jpg", "haighschocolate.jpg", "palominoespresso.jpg", "upstate.jpg", "traif.jpg", "grahamavenuemeats.jpg", "wafflewolf.jpg", "fiveleaves.jpg", "cafelore.jpg", "confessional.jpg", "barrafina.jpg", "donostia.jpg", "royaloak.jpg", "thaicafe.jpg"] 

var restaurantLocations = ["Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Sydney", "Sydney", "Sydney", "New York", "New York", "New York", "New York", "New York", "New York", "New York", "London", "London", "London", "London"] 

var restaurantTypes = ["Coffee & Tea Shop", "Cafe", "Tea House", "Austrian/Causual Drink", "French", "Bakery", "Bakery", "Chocolate", "Cafe", "American/Seafood", "American", "American", "Breakfast & Brunch", "Coffee & Tea", "Coffee & Tea", "Latin American", "Spanish", "Spanish", "Spanish", "British", "Thai"] 


override func viewDidLoad() { 
    super.viewDidLoad() 

    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

// MARK: - Table view data source 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // Return the number of rows in the section. 
    return self.restaurantNames.count 
} 

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

    let cellIdentifier = "Cell" 
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CustomTableViewCell 

    // Configure the cell... 
    cell.nameLabel.text = restaurantNames[indexPath.row] 
    cell.thumbnailImageView.image = UIImage(named: restaurantImages[indexPath.row]) 
    cell.locationLabel.text = restaurantLocations[indexPath.row] 
    cell.typeLabel.text = restaurantTypes[indexPath.row] 

    // Circular image 
    // cell.thumbnailImageView.layer.cornerRadius = 10.0 
    cell.thumbnailImageView.layer.cornerRadius =  cell.thumbnailImageView.frame.size.width/2 
    cell.thumbnailImageView.clipsToBounds = true 

    if restaurantIsVisited[indexPath.row] { 
     cell?.accessoryType = UITableViewCellAccessoryType.Checkmark 
    } else { 
     cell?.accessoryType = UITableViewCellAccessoryType.None 
    } 

    return cell 
} 

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    let optionMenu = UIAlertController(title: nil, message: "What do you want to do?", preferredStyle: .ActionSheet) 

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil) 
    optionMenu.addAction(cancelAction) 

    self.presentViewController(optionMenu, animated: true, completion: nil) 

    let callActionHandler = { (action: UIAlertAction!) -> Void in 

     let alertMessage = UIAlertController(title: "Service Unavailable", message: "Sorry", preferredStyle: UIAlertControllerStyle.Alert) 
     alertMessage.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) 
     self.presentViewController(alertMessage, animated: true, completion: nil) 
    } 

    let callAction = UIAlertAction(title: "Call" + "123-000-\(indexPath.row)", style: .Default, handler: nil) 
    let isVisitedAction = UIAlertAction(title: "I've been here", style: UIAlertActionStyle.Default, handler: { 
     (action:UIAlertAction!) -> Void in 


     cell?.accessoryType = .Checkmark 
     self.restaurantIsVisited[indexPath.row] = true 
    }) 
    optionMenu.addAction(isVisitedAction) 
} 

而Xcode表示它沒有一個名爲「accessoryType」的成員 另外它會要求我刪除問號。如何在Swift中使用accesoryType?

下面是CustomTableViewCell代碼:

import UIKit 

class CustomTableViewCell: UITableViewCell { 
@IBOutlet weak var nameLabel: UILabel! 
@IBOutlet weak var locationLabel: UILabel! 
@IBOutlet weak var typeLabel: UILabel! 
@IBOutlet weak var thumbnailImageView: UIImageView! 

override func awakeFromNib() { 
    super.awakeFromNib() 
    // Initialization code 
} 

override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

    // Configure the view for the selected state 
} 

} 

這是沒用的,頁面要求我增加更多的細節,我不知道是什麼補充,所以... 更多的細節,更多詳細信息,更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,更多細節更多細節更多細節更多細節更多細節更多細節更多細節更多細節更多細節更多細節更多細節更多細節更多細節更多細節更多細節更多細節更多細節,莫更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,更多詳情,

+1

何處/如何定義cell?請顯示一個*獨立的*示例來說明問題。 –

+2

和'.CheckMark'應該是'.Checkmark' ... –

+0

*確切的完整錯誤信息*也會有幫助。 –

回答

2

您可以通過以下方式在swift中設置配件類型。

cell.accessoryType = UITableViewCellAccessoryType.None 
    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator 
    cell.accessoryType = UITableViewCellAccessoryType.DetailDisclosureButton 

這可以幫助你。

+0

這不是必需的,並不能解釋錯誤信息。 'cell.accessoryType = .None'編譯沒有問題(如果單元格確實是一個UITableViewCell)。 –

+0

這個答案沒有幫助或回答問題,因爲@MartinR已經說過,如果它是OP所使用的'UITableViewCell','.None'應該沒有'UITableViewCellAccessoryType'前綴 – milo526

0

因爲你的語法似乎是正確的

var cell = UITableViewCell(frame: CGRectMake(0, 0, 200, 200)) 

cell.accessoryType = UITableViewCellAccessoryType.Checkmark 
cell.accessoryType = .Checkmark 

雙方似乎在Xcode測試版6.3工作使用雨燕1.2

我只能推測是你的不正確初始化或不初始化爲一個UITableViewCell

Swift中的另一種類型的單元格是UICollectionViewCell

var newCell = UICollectionViewCell(frame: CGRectMake(0, 0, 200, 200)) 

newCell.accessoryType = UICollectionViewCellAccessorryType.Checkmark 
因此

UICollectionViewCell.accessoryType
使用UIColectionViewCellAccessoryType可能。
試圖訪問.accessoryTypeUICollectionViewCell將導致錯誤’UICollectionViewCell’ does not have a member named ‘accessoryType’

這是一個例子,如果你使用其他類型的電池,請確認您的細胞類型確實有.accessoryType

試一試,看是否你在初始化的過程中做這件事

+0

謝謝!單元格初始化爲CustomTableViewCell,並且出現錯誤,提示「使用未解析的標識符單元格」 –

+0

請問您可以添加CustomTableViewCell的代碼嗎? (編輯你的問題,並在那裏添加它) – milo526

+0

現在它被添加。 –

相關問題