2016-05-17 116 views
0

我認爲這可能是X Y的問題,所以我會先給出一些背景信息。如何更優雅地添加/刪除表格視圖中的單元格?

我正在構建一個可以顯示「窗體」的應用程序。用戶可以用一些東西填寫表單並創建一些自定義的東西。

我認爲最合適的做法是爲表格使用表格視圖。我可以在每個表格單元格中顯示需要填寫的所有文本框。

以下是截圖:

enter image description here

「添加新輸入」按鈕,將插入一個新的電池在底部時,它被竊聽。如果您將其中一個輸入滑動到左側,您將看到一個「刪除」按鈕。您可以使用它來刪除輸入。

如您所見,此表視圖需要添加和刪除行。

最初,我使用了一個名爲「TableViewModel」的cocoapod,這使得這非常簡單。但後來我在庫中找到了a really severe bug,所以我不想再使用它了。

我嘗試使用表視圖的deleteRowsAtIndexPathsinsertRowsAtIndexPaths方法。但是,如果我不喜歡這樣寫道:

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

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = UITableViewCell() 
    cell.textLabel?.text = "Hello" 
    return cell 
} 

// I use motionEnded here because I don't want to add a button or anything just to test this 
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) { 
    tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Fade) 
} 

這將導致一個異常說,表視圖與數據源不一致之後我刪除一行。這意味着我必須有代碼來處理這種不一致。這肯定會讓我的代碼更難閱讀。

插入方法也是一樣。

此外,我需要跟蹤每個部分有多少行。我的代碼變得非常混亂,無法維護。

我也搜索過其他庫,但它們都很奇怪,並不像「tableViewModel」那麼簡單。他們似乎都要求我爲表視圖創建一個模型。我不明白如何做到這一點。我只想顯示一堆文本字段!

如何更優雅地插入或刪除行?我認爲我需要編寫表視圖的extension或學習爲我的表視圖編寫模型。但是我能夠完成這兩種方法。

+0

因爲您沒有任何數據源。另請參閱邏輯:假設您使用insertRowsAtIndexPaths添加了一個新行。所以現在你有兩排。然而你的numberOfRowsInSection總是返回1,所以它會崩潰,反之亦然。表視圖應該與集合(NSArray,NSDictionary,NSSet等)一起工作。 – Alok

+0

事情是,當你添加或刪除一行時,你總是說在該部分只有一行。您必須使用數組或字典來跟蹤每行和每個部分中的數據(和哪些數據),並在'numberOfSectionsInTableView:'和'tableView:'numberOfRowsInSection:'中返回該值。 – Larme

+0

你的意思是我不應該使用表視圖來做這種表單?那我能做些什麼呢? @Alok – Sweeper

回答

0

我找到了解決方案!

我基本上保持細胞的爲表視圖到顯示陣列的陣列:

var cells: [[UITableViewCell]] = [[], [], []] // I have 3 sections, so 3 empty arrays 

然後我加入這些數據源的方法:

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

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return cells[section].count 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    return cells[indexPath.section][indexPath.row] 
} 

現在,我可以添加和輕鬆移除細胞:

func addCellToSection(section: Int, index: Int, cell: UITableViewCell) { 
    cells[section].insert(cell, atIndex: index) 
    tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: index, inSection: section)], withRowAnimation: .Left) 
} 

func removeCellFromSection(section: Int, index: Int) { 
    cells[section].removeAtIndex(index) 
    tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: index, inSection: section)], withRowAnimation: .Left) 
} 

只需兩行,我就可以添加/刪除帶有動畫的單元格!

0

因爲您沒有任何data source。另請參閱logically:假設您使用insertRowsAtIndexPaths添加了一個新行。所以現在你有2 rows。然而你的numberOfRowsInSection總是返回1.所以它會崩潰,反之亦然。表視圖應與collectionNSArray,NSDictionary,NSSet等)一起使用。 對你有所幫助:

Already they have nade a form as yours

obj-c easy to understand how table view with data source work

Adding, Updating, Deleting and Moving records using Swift.

0

你可以使用一個TableView創建形式,但你必須設計一個合適的數據源來跟蹤這些數據。現在如下

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    if editingStyle == .Delete { 
     //Delete the row from the data source to ensure consistency 
     self.array.removeAtIndex(indexPath.row) 
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
    } 
} 

你可以使用一個模型類爲dataSource的數據源方法必須進行修改,以

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

您可以編輯這個數據源當執行刪除操作。

class NewCustomOperation: NSObject { 
    var name: String? 
    var rejectFloatingPoints: Bool? 
    var inputs: AvailableInputs? 
    var results: Results? 
} 

class AvailableInputs: NSObject { 
    var name: [String]? 
    var description: [String]? 
} 

class Results: NSObject { 
    var name: [String]? 
    var formula: [String]? 
} 
+0

是的,我知道,但如果我有一堆用戶填寫的文本框,而不是一堆要顯示的數據,那麼數據源會是什麼? – Sweeper

+0

你有沒有考慮過使用堆棧視圖?也許很多工作,但他們會爲你管理你的界面非常流暢。 – user3069232

+0

@ user3069232但我使用iOS8,但我不認爲堆棧視圖可用。 – Sweeper

相關問題