***更新:耶!它现在有效!***我一直在寻找几个小时,但还没有找到我想要解决的问题。有一些答案在那里使用目标c,但我正在尝试Swift,我似乎无法得到翻译的代码。 (打赌你不能说我对此有多新!)我有一个简单的tableView
,它显示了一个数组中的传输类型列表。按下每个单元格时,会弹出一个警告,询问用户之前是否使用过这种类型的运输。他们可以回答是或否。如果答案是肯定的,那么我想取消隐藏tableView
中的一个标签。好像应该这么简单,但我正在撕扯我的头发。 tableview工作完美,警报工作完美;我似乎无法取消隐藏tableView
中的标签。我想我需要在某个地方重新加载tableView
,但我一直都会收到错误,说我不建议这样做。
这些是我的自定义单元格类中的出口:
class TransportCell: UITableViewCell {
@IBOutlet weak var transportationTitleLabel: UILabel!
@IBOutlet weak var typeLabel: UILabel!
@IBOutlet weak var transportationImage: UIImageView!
}
这是我的tableviewcontroller:
class TransportationTableViewController: UITableViewController {
struct TransportItem
{
var label: String
var subTitle: String
var isHidden: Bool
}
let TransportationItems = [TransportItem(label: "Bus", subTitle: "I have used this type before.", isHidden: true),
TransportItem(label: "Helicopter", subTitle: "I have used this type before.", isHidden: false),
TransportItem(label: "Truck", subTitle: "I have used this type before.", isHidden: true),
TransportItem(label: "Boat", subTitle: "I have used this type before.", isHidden: true),
TransportItem(label: "Bicycle", subTitle: "I have used this type before.", isHidden: true),
TransportItem(label: "Motorcycle", subTitle: "I have used this type before.", isHidden: true),
TransportItem(label: "Plane", subTitle: "I have used this type before.", isHidden: true),
TransportItem(label: "Train", subTitle: "I have used this type before.", isHidden: true),
TransportItem(label: "Car", subTitle: "I have used this type before.", isHidden: true),
TransportItem(label: "Scooter", subTitle: "I have used this type before.", isHidden: true),
TransportItem(label: "Caravan", subTitle: "I have used this type before.", isHidden: true)]
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return TransportationItems.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:TransportCell = tableView.dequeueReusableCellWithIdentifier("transportCell", forIndexPath: indexPath) as TransportCell
var item = TransportationItems[indexPath.row]
cell.transportationTitleLabel.text = item.label
cell.typeLabel.text = item.subTitle
cell.typeLabel.hidden = item.isHidden
var imageName = UIImage(named:item.label)
cell.transportationImage.image = imageName
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
var item = self.TransportationItems[indexPath.row]
let alert = UIAlertController(title: "Used this transport?", message: "You selected \(item.label)", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.Cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: { alertAction in
println(item.label)
println(item.subTitle)
var customCell = self.tableView.cellForRowAtIndexPath(indexPath) as TransportCell
customCell.typeLabel.hidden = false
}))
self.presentViewController(alert, animated: true, completion: nil)
}
答案 0 :(得分:0)
修改您的数据源(transportationItems
)以执行struct
而不是String
:
struct TransportItem {
var label: String
var hidden: Bool
}
let transportationItems = [
TransportItem(label: "Bus", hidden: true),
........
TransportItem(label: "Caravan", hidden: true)
]
然后转到cellForRowAtIndexPath
,然后执行:
var item = transportationItems[indexPath.row]
cell.typeLabel?.text = item.label
cell.typeLabel?.hidden = item.hidden
最后:
// THIS IS WHERE YOU LOST
// REMOVE THE CODE YOU HAVE, AND DO THIS
var item = self.transportationItems[indexPath.row]
item.hidden = false
self.tableView.reloadData()