正在输出相同的自定义单元格 (Swift)
Identical Custom Cells being output (Swift)
我正在尝试创建自定义 TableView
,它将根据 Dictionary
中包含的键输出自定义单元格。我已经为每个自定义单元格创建了 类 和出口,但是当我构建和 运行;多次显示相同的自定义单元格。我显示的单元格数量正确(即与字典中的键数相同)但我似乎无法区分输出的单元格。
这是我的代码:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataDict.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if (dataDict.indexForKey("Number") != nil) {
let firstcell:MyFirstCell = self.scanConfirmTable.dequeueReusableCellWithIdentifier("scanfirst", forIndexPath: indexPath) as! MyFirstCell
return firstcell
}
else if (dataDict.indexForKey("Social") != nil) {
let secondcell:MySecondCell = self.scanConfirmTable.dequeueReusableCellWithIdentifier("scansecond", forIndexPath: indexPath) as! MySecondCell
return secondcell
}
else {
let emptycell:ScanEmptyCell = self.scanConfirmTable.dequeueReusableCellWithIdentifier("scanemptycell", forIndexPath: indexPath) as! ScanEmptyCell
return emptycell
}
我在这里搜索了以前的帖子,找到了一个使用类似内容的选项:
let currentTag = dataDict[indexPath.row]
但是我收到一个错误:
Cannot subscript a value of type '[String:String]' with an index type 'Int'.
如有任何帮助,我们将不胜感激!
而不是使用 Dictionary
尝试使用包含所有键的 Array
排序并将该数组与 tableViewDataSource
方法一起使用。
var keysArray = [String]()
keysArray = Array(dataDict.keys).sorted(<)
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.keysArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if (keysArray[indexPath.row] == "Number") {
let firstcell:MyFirstCell = self.scanConfirmTable.dequeueReusableCellWithIdentifier("scanfirst", forIndexPath: indexPath) as! MyFirstCell
return firstcell
}
else if (keysArray[indexPath.row] == "Social") {
let secondcell:MySecondCell = self.scanConfirmTable.dequeueReusableCellWithIdentifier("scansecond", forIndexPath: indexPath) as! MySecondCell
return secondcell
}
else {
let emptycell:ScanEmptyCell = self.scanConfirmTable.dequeueReusableCellWithIdentifier("scanemptycell", forIndexPath: indexPath) as! ScanEmptyCell
return emptycell
}
我正在尝试创建自定义 TableView
,它将根据 Dictionary
中包含的键输出自定义单元格。我已经为每个自定义单元格创建了 类 和出口,但是当我构建和 运行;多次显示相同的自定义单元格。我显示的单元格数量正确(即与字典中的键数相同)但我似乎无法区分输出的单元格。
这是我的代码:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataDict.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if (dataDict.indexForKey("Number") != nil) {
let firstcell:MyFirstCell = self.scanConfirmTable.dequeueReusableCellWithIdentifier("scanfirst", forIndexPath: indexPath) as! MyFirstCell
return firstcell
}
else if (dataDict.indexForKey("Social") != nil) {
let secondcell:MySecondCell = self.scanConfirmTable.dequeueReusableCellWithIdentifier("scansecond", forIndexPath: indexPath) as! MySecondCell
return secondcell
}
else {
let emptycell:ScanEmptyCell = self.scanConfirmTable.dequeueReusableCellWithIdentifier("scanemptycell", forIndexPath: indexPath) as! ScanEmptyCell
return emptycell
}
我在这里搜索了以前的帖子,找到了一个使用类似内容的选项:
let currentTag = dataDict[indexPath.row]
但是我收到一个错误:
Cannot subscript a value of type '[String:String]' with an index type 'Int'.
如有任何帮助,我们将不胜感激!
而不是使用 Dictionary
尝试使用包含所有键的 Array
排序并将该数组与 tableViewDataSource
方法一起使用。
var keysArray = [String]()
keysArray = Array(dataDict.keys).sorted(<)
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.keysArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if (keysArray[indexPath.row] == "Number") {
let firstcell:MyFirstCell = self.scanConfirmTable.dequeueReusableCellWithIdentifier("scanfirst", forIndexPath: indexPath) as! MyFirstCell
return firstcell
}
else if (keysArray[indexPath.row] == "Social") {
let secondcell:MySecondCell = self.scanConfirmTable.dequeueReusableCellWithIdentifier("scansecond", forIndexPath: indexPath) as! MySecondCell
return secondcell
}
else {
let emptycell:ScanEmptyCell = self.scanConfirmTable.dequeueReusableCellWithIdentifier("scanemptycell", forIndexPath: indexPath) as! ScanEmptyCell
return emptycell
}