2016-02-14 90 views
2

有沒有什麼辦法可以像日曆事件創建中的那樣快速地製作簡單的Accordion View?我不想使用其他第三方庫以及其他代碼。在swift中製作簡單的Accordion TableView?

我在github和google上找到了很多答案。但是,仍然不符合我的要求。

其實我想添加兩個表格視圖。

一是部分,其表現出城,如(紐約,洛杉磯,拉斯維加斯等)

當我拍了拍的城市之一,它會顯示在tableview中存儲地址這意味着有很多商店。

所有的商店和數據將從json獲得。

手風琴視圖,我想要做的就像在iOS上的日曆應用程序一樣簡單。但是,我要插入到兩個tableView中的數據(每個段內的段頭& Inner Records)是動態的。

任何幫助?請引導我,幫助我。

UPDATE:請看看

enter image description here

+0

你可以添加一個屏幕截圖來更好地瞭解你 – TechBee

+0

我已經更新了這個問題。一個城市可能會有很多店鋪地址不同。 –

回答

3

由@TechBee提供的答案工作正常使用的部分爲那些有興趣在不使用的部分,並使用電池。

摺疊式菜單在斯威夫特的實現可以在一個非常簡單的方法使用UITableView,只是有兩個單元之一的親本細胞,另一個孩子的細胞,並在每一個瞬間可以實現保持跟蹤的細胞展開或摺疊,因爲每次新單元格展開或摺疊時它都可以更改indexPath.row

,用函數的insertRowsAtIndexPaths(_:withRowAnimation:)deleteRowsAtIndexPaths(_:withRowAnimation:)總是呼叫到tableView.beginUpdates()tableView.endUpdates()一個塊內,總項目的更新數據源或模擬它改變我們可以在一個非常實現新細胞的缺失的插入在UITableView包括動畫的簡單方法。

我已經在Github上實現了一個倉庫,上面解釋了上述AccordionMenu使用Swift和UITableView的一個簡單易懂的方法。它允許擴展多個單元或者只允許一個單元。

+0

感謝您的簡單易懂,並感謝您的一些定製 –

+0

@ 6245Htarwara當然,即使你可以看到存儲庫中的例子我已經在答案中看到它在行動:) –

2

試試這個:

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    arrayForBool = ["0","0","0"] 
    sectionTitleArray = ["Pool A","Pool B","Pool C"] 
    var tmp1 : NSArray = ["New Zealand","Australia","Bangladesh","Sri Lanka"] 
    var string1 = sectionTitleArray .objectAtIndex(0) as? String 
    [sectionContentDict .setValue(tmp1, forKey:string1!)] 
    var tmp2 : NSArray = ["India","South Africa","UAE","Pakistan"] 
    string1 = sectionTitleArray .objectAtIndex(1) as? String 
    [sectionContentDict .setValue(tmp2, forKey:string1!)] 


    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell") 


} 

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

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return sectionTitleArray.count 
} 


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{ 

    if(arrayForBool .objectAtIndex(section).boolValue == true) 
    { 
     var tps = sectionTitleArray.objectAtIndex(section) as! String 
     var count1 = (sectionContentDict.valueForKey(tps)) as! NSArray 
     return count1.count 
    } 
    return 0; 
} 

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return "ABC" 
} 

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 

    return 50 
} 

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { 
    return 1 
} 

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    if(arrayForBool .objectAtIndex(indexPath.section).boolValue == true){ 
     return 100 
    } 

    return 2; 
} 

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

    let headerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40)) 
    headerView.backgroundColor = UIColor.grayColor() 
    headerView.tag = section 

    let headerString = UILabel(frame: CGRect(x: 10, y: 10, width: tableView.frame.size.width-10, height: 30)) as UILabel 
    headerString.text = sectionTitleArray.objectAtIndex(section) as? String 
    headerView .addSubview(headerString) 

    let headerTapped = UITapGestureRecognizer (target: self, action:"sectionHeaderTapped:") 
    headerView .addGestureRecognizer(headerTapped) 

    return headerView 
} 

func sectionHeaderTapped(recognizer: UITapGestureRecognizer) { 
    println("Tapping working") 
    println(recognizer.view?.tag) 

    var indexPath : NSIndexPath = NSIndexPath(forRow: 0, inSection:(recognizer.view?.tag as Int!)!) 
    if (indexPath.row == 0) { 

     var collapsed = arrayForBool .objectAtIndex(indexPath.section).boolValue 
     collapsed  = !collapsed; 

     arrayForBool .replaceObjectAtIndex(indexPath.section, withObject: collapsed) 
     //reload specific section animated 
     var range = NSMakeRange(indexPath.section, 1) 
     var sectionToReload = NSIndexSet(indexesInRange: range) 
     self.tableView .reloadSections(sectionToReload, withRowAnimation:UITableViewRowAnimation.Fade) 
    } 

} 

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

    let CellIdentifier = "Cell" 
    var cell :UITableViewCell 
    cell = self.tableView.dequeueReusableCellWithIdentifier(CellIdentifier) as! UITableViewCell 

    var manyCells : Bool = arrayForBool .objectAtIndex(indexPath.section).boolValue 

    if (!manyCells) { 
     // cell.textLabel.text = @"click to enlarge"; 
    } 
    else{ 
     var content = sectionContentDict .valueForKey(sectionTitleArray.objectAtIndex(indexPath.section) as! String) as! NSArray 
     cell.textLabel?.text = content .objectAtIndex(indexPath.row) as? String 
     cell.backgroundColor = UIColor .greenColor() 
    } 

    return cell 
} 
+0

我知道這個例子。那是來自github。但是,我之所以問這個是因爲我想從領域swift對象中添加動態數據。如果你需要知道,我會更新這個問題。 –

+0

只更改領域的數據部分。你的商業邏輯不應該改變。 – TechBee

+0

好吧,我會試一試。如果這樣做不起作用,我會讓你知道。還有其他方法嗎?像日曆事件添加? –