【发布时间】:2016-07-31 21:43:15
【问题描述】:
我正在尝试创建一个动态表格视图。现在我得到了一组未排序的运动对象(其中一些是相同的,但记录的信息不同)。
我首先需要为添加到日志中的每个独特练习创建一个部分。
之后我需要将所有练习添加到特定部分,因此“卧推”的记录练习位于“卧推”部分等下。
现在它说 - 当我尝试分配标签时,TmpExercise 没有下标成员。
有没有更好的方法来做到这一点?
我已经研究了几个小时的不同解决方案,所以我希望你们中的一些人可以帮助我:-)。
struct Objects {
var sectionName : String!
var sectionObjects : [TmpExercise]!
}
var objectArray = [Objects]()
var tmpArray = [TmpExercise()]
var exerciseDictionary = Dictionary<String, TmpExercise>()
override func viewDidLoad() {
super.viewDidLoad()
for exercise in log!.tmpExercises {
exerciseDictionary[(exercise.tmpExercise!.name)] = exercise
}
for (key, value) in exerciseDictionary {
tmpArray = [TmpExercise()]
for exercise in log!.tmpExercises {
if exercise.tmpExercise!.name == key {
tmpArray.append(exercise)
}
}
objectArray.append(Objects(sectionName: key, sectionObjects: tmpArray))
tmpArray.removeAll()
}
}
override func viewDidAppear(animated: Bool) {
tableView.reloadData()
}
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return objectArray.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objectArray[section].sectionObjects.count-1
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return objectArray[section].sectionName
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Log Detail", forIndexPath: indexPath)// as! UITableViewCell
cell.textLabel!.text = objectArray[indexPath.section].sectionObjects[indexPath.row]
// TmpExercise has no subscript members
return cell
}
编辑: 我已经尝试实现@Mohamed Mostafa 的答案,请查看我整个班级的以下代码。
我现在已经上传了内容的图片。硬拉有两个细胞就可以了,但深蹲有 5 个细胞,应该只包含 3 个细胞。深蹲下的前两个单元叫做硬拉,其余的都是正确的。
谁能告诉我我做错了什么?
struct Objects {
var sectionName : String!
var sectionObjects : [TempExercise]!
}
struct TempExercise {
var Name : String!
}
class SelectedLogViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var log: Log?
var objectArray = [Objects]()
var tmpObjectArray = [Objects]()
var tmpArray = [TempExercise()]
var test: Objects?
var exerciseDictionary = Dictionary<String, String>()
override func viewDidLoad() {
super.viewDidLoad()
for exercise in log!.tmpExercises {
exerciseDictionary[(exercise.tmpExercise!.name)] = exercise.tmpExercise!.name
}
for (key, value) in exerciseDictionary {
tmpArray = [TempExercise(Name:value)]
objectArray.append(Objects(sectionName: key, sectionObjects: tmpArray))
tmpArray.removeAll()
}
for uniqueExercise in objectArray {
for logExercise in log!.tmpExercises {
if uniqueExercise.sectionName == logExercise.tmpExercise!.name {
let tmpEx = TempExercise(Name: logExercise.tmpExercise!.name)
tmpArray.append(tmpEx)
}
test = Objects(sectionName: uniqueExercise.sectionName, sectionObjects: tmpArray)
}
tmpObjectArray.append(test!)
test?.sectionObjects.removeAll()
}
}
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return objectArray.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tmpObjectArray[section].sectionObjects.count
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return objectArray[section].sectionName
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Log Detail", forIndexPath: indexPath)// as! UITableViewCell
cell.textLabel!.text = tmpObjectArray[indexPath.section].sectionObjects[indexPath.row].Name
return cell
}
}
【问题讨论】:
标签: ios iphone swift xcode uitableview