0

***更新:耶!它现在可以工作了!***我已经四处寻找了几个小时,但还没有找到我需要弄清楚的确切内容。使用objective c有一些答案,但我正在尝试Swift,但似乎无法翻译代码。(打赌你不知道我对此有多陌生!)我有一个简单的tableView方法,它显示了一个数组中的运输类型列表。当每个单元格被按下时,会弹出一个警报,询问用户他们以前是否使用过这种交通工具。他们可以回答是或否。如果答案是肯定的,那么我想取消隐藏我的tableView. 似乎它应该如此简单,但我正在扯掉我的头发。表格视图完美运行,警报完美运行;我似乎无法在我的tableView.tableView某处,但我不断收到错误,说不推荐我正在做的事情。

这些是我的自定义单元类中的插座:

class TransportCell: UITableViewCell {

@IBOutlet weak var transportationTitleLabel: UILabel!
@IBOutlet weak var typeLabel: UILabel!
@IBOutlet weak var transportationImage: UIImageView!
}

这是我的表视图控制器:

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)
}
4

1 回答 1

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()
于 2014-11-07T05:40:13.393 回答