2015-09-07 67 views
0

我試圖實現一項功能,其中如果用戶單擊我的UICollectionView(即嵌入在UITableViewCell中)中的某個項目,則會導致另一部分出現在UITableViewUICollectionView下方)與關於那個項目的信息。我遇到單擊時出現的部分高度問題。如果我最初在heightForRowAtIndexPath中將其設置爲0,那麼稍後似乎沒有辦法改變單元格高度。我嘗試給單元格初始高度,然後用cell.hidden隱藏它,但仍然可以看到該部分。也許有另一種方式來做到這一點,但經過大量的谷歌搜索,我很快就沒有了。隱藏和展開UITableView部分

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    if indexPath.section == 3 { 
     return 90 
    } 
    else if indexPath.section == 1 { 
     return 185 
    } 
    else if indexPath.section == 2 { 
     return 155 
    } 
    else if indexPath.section == 4 { 
     return 100 
    } 
    return UITableViewAutomaticDimension 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     ... 
    } 
    else if indexPath.section == 3 { 
     let cell = tableView.dequeueReusableCellWithIdentifier("weeklyCell", forIndexPath: indexPath) as! WxTableViewCell 
     cell.selectionStyle = UITableViewCellSelectionStyle.None 
     cell.forecastCollectionView.delegate = self 
     cell.forecastCollectionView.dataSource = self 
     cell.forecastCollectionView.reloadData() 
     cell.forecastCollectionView.tag = indexPath.row 
     cell.forecastCollectionView.showsHorizontalScrollIndicator = false 
     return cell 
    } 
    else if indexPath.section == 4 { 
     let cell = tableView.dequeueReusableCellWithIdentifier("dailyInformation", forIndexPath: indexPath) as! WxTableViewCell 
     cell.hidden = true 
     cell.contentView.hidden = true 
     return cell 
    } 
    ... 
} 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    let index = NSIndexPath(forRow: 0, inSection: 3) 
    let myindex = NSIndexPath(forRow: 0, inSection: 4) 
    var myTable = self.tableView 
    var lastCell = myTable.cellForRowAtIndexPath(index) 
    var dailyCell = myTable.cellForRowAtIndexPath(myindex) 
    var myView = UIView(frame: CGRectMake(20, 0, lastCell!.frame.width, lastCell!.frame.height)) 
    dailyCell.frame.size = myView.frame.size 
} 
+1

您應該避免嘗試更改其他單元格的單元格。只需在一個單元格中執行操作即可更新數據模型,然後重新加載可能受影響的部分。假設你有你的代碼正確的單元格創建和返回高度,他們會照顧自己。所以過程應該是:單元格中的單元格或項目被單擊 - >更新數據模型 - >重新加載受影響的部分或行或整個表格。 –

回答

1

切勿直接使用indexPath編號來尋址開關/外殼中的單元和部分。當你想在中間添加/刪除一行/部分時很難處理。相反,創建一個包含所有部分的枚舉,並使用包含要顯示的部分的數組。您可以在該枚舉中擁有單元格或部分屬性,例如單元格標識符或高度。

enum FileDetailsCell: Printable { 
    case FileName, Location, Size 

    var description: String { 
     switch self { 
     case .FileName: return "noname" 
     case .Location: return"Location" 
     case .Size: return "Size" 
     } 
    } 

    var defaultValue: String { 
     switch self { 
     case .Location: return "/" 
     case .Size: return "Unknown" 
     default: return "" 
     } 
    } 

    var cellIdentifier: String { 
     switch self { 
     case .FileName: return "fileNameCell" 
     default: return "detailCell" 
     } 
    } 
} 

這裏我使用單元格標題的描述。用於此目的的cell和cellIdentifier初始值的defaultValue是顯而易見的。

然後定義它含有活性細胞陣列:

var cellsArray: [FileDetailsCell] = [.FileName, .Size] 

當你想更改可見單元格,你可以修改此數組。現在,位置單元格是隱藏的,但您可以通過將「.Location」附加到此數組並重新加載tableView來使其可見。

對於的tableview委託:

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

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cellInfo = cellsArray[indexPath.row] 

    let cell = tableView.dequeueReusableCellWithIdentifier(cellInfo.cellIdentifier, forIndexPath: indexPath) as! UITableViewCell 
    switch cellInfo { 
     case .FileName: 
      cell.textLabel?.text = item.fileName; 
     case .Location: 
      cell.textLabel?.text = cellInfo.description 
      cell.detailTextLabel?.attributedText = item.locationFormatted ?? cellInfo.defaultValue; 
     case .Size: 
      cell.textLabel?.text = cellInfo.description 
      cell.detailTextLabel!.text = item.size ?? cellInfo.defaultValue; 
    } 

    return cell 
} 

在這個例子中我假定部分是僅一個,以避免混亂。對於部分你可以得到類似的方法。