Ios 如何在Swift中使用accesoryType?
Xcode说它没有名为“accessoryType”的成员 它还要求我删除问号 以下是CustomTableViewCell的代码:Ios 如何在Swift中使用accesoryType?,ios,xcode,swift,Ios,Xcode,Swift,Xcode说它没有名为“accessoryType”的成员 它还要求我删除问号 以下是CustomTableViewCell的代码: import UIKit class RestaurantTableViewController: UITableViewController, UITableViewDelegate { var restaurantIsVisited = [Bool](count: 21, repeatedValue: false) var restaurantNames
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)
}
这是无用的,页面要求我添加更多细节,我不知道要添加什么,所以。。。
更多细节,更多细节,更多细节,更多细节,更多细节,更多细节,更多细节,更多细节,更多细节,更多细节,更多细节,更多细节,更多细节,更多细节,更多细节,更多细节,更多细节,更多细节,更多细节,更多细节,更多细节,更多细节,更多细节,更多详情,更多详情,更多详情,更多详情,更多详情,更多详情,更多详情,更多详情,更多详情,更多详情,更多详情,更多详情,更多详情,更多详情,更多详情,更多详情,更多详情,更多详情,更多详情,更多详情,更多详情,更多详情,更多详情,更多详情,您可以在swift中按照以下方式设置附件类型
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
}
}
希望这对您有所帮助。因为您的语法似乎正确
cell.accessoryType = UITableViewCellAccessoryType.None
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
cell.accessoryType = UITableViewCellAccessoryType.DetailDisclosureButton
两者似乎都在使用Swift 1.2的xCode Beta 6.3中工作
我只能判断您的单元格未正确初始化或未初始化为UITableViewCell
Swift中的另一种单元格类型是UICollectionViewCell
var cell = UITableViewCell(frame: CGRectMake(0, 0, 200, 200))
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
cell.accessoryType = .Checkmark
UICollectionViewCell
没有的.accessoryType
因此,使用UICollectionViewCellAccessoryType是不可能的。
试图在UICollectionViewCell
上访问.accessoryType
将导致错误'UICollectionViewCell'没有名为'accessoryType'的成员。
这是一个示例,如果您的单元格使用其他类型,请验证您的单元格类型是否具有.accessoryType
尝试查看您是否在执行此操作时进行初始化在哪里/如何定义单元格
?请展示一个演示问题的自包含示例。并且。CheckMark
应该是。CheckMark
…准确的完整错误消息也会有帮助。Swift编译器有两个错误,一个是使用未解析的标识符“cell”,另一个是后缀“”的操作数“”应该具有可选类型,类型是“CustomTableViewCell”在问题的初始版本中,您报告了一条错误消息“没有名为accessoryType的成员”。现在您有不同的错误消息了吗cell?.accessoryType
应该是cell.accessoryType
,并且在DidSelectRowatineXpath方法中,没有cell
变量,因此它当然是未定义的。这不是必需的,也不能解释错误消息cell.accessoryType=.None
编译没有问题(如果cell确实是一个UITableViewCell)。正如@MartinR已经指出的那样,如果OP使用的是一个UITableViewCell
,这个答案不会帮助或回答这个问题。如果没有UITableViewCellAccessoryType
前缀,None
应该可以工作!该单元格已初始化为CustomTableViewCell,出现一个错误,提示“使用未解析标识符‘cell’”。请添加CustomTableViewCell的代码,好吗?(编辑您的问题并将其添加到那里)抱歉,这是来自您的类的代码,我从您的CustomTableViewCell请求了代码,还是您使用故事板创建了它们?
var newCell = UICollectionViewCell(frame: CGRectMake(0, 0, 200, 200))
newCell.accessoryType = UICollectionViewCellAccessorryType.Checkmark