C
Easy to summarize andtableView(_:cellForRowAtIndexPath:)I don't know when to call it, so I need to write the code so that it can be called whenever.If you say, for example, if you have a table of 10 lines, you will see a part from the 6th to 10th line. When the user scroll operation needs to re-display lines 1 to 5UITableViewHomeUITableViewDataSourceOn the other hand, in the sense that "you can do the contents of the first line to the fifth line"index.rowChange the value to 0...4, but 5 timestableView(_:cellForRowAtIndexPath:)call. String created as a subtitle at that timeString(format: "%.f回", y)Insideyfor 1 to 5 linesynot) "Last calculatedyThe same value is all subtitles from the first line to the fifth line.So, if you want to display the subtitle correctly, you should not have the same number of data used for the subtitle.First,todoItemadd an array of subtitles to where it is場所d (this is also wanted to include in the code, but it is not found). var todoSubitem: [String] = []
todoItemHometodoSubitemSince the number of elements must not be matched at all times,onClickMyButton2(_:)You can use the method. func onClickMyButton2(sender: UIButton){
todoItem.append(self.myTextField3.text!)
todoSubitem.append(String(format: "%.f回", y))
NSUserDefaults.standardUserDefaults().setObject(todoItem, forKey: "todoList")
//必要ならtodoSubitemも保存
// TableViewを再読み込み.
myTableView.reloadData()
}
Changes to "todoItemHometodoSubitemIt should be "to hold the data of the number of lines in the table", and thentableView(_:cellForRowAtIndexPath:)Also, if you rewrite it accordingly, you say. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
// Cellに値を設定
cell.textLabel?.text = todoItem[indexPath.row]
cell.detailTextLabel?.text = todoSubitem[indexPath.row]
return cell
}
In the future, even if you need to add more functions and delete or replace the order, "todoItemIf you specify the contentstodoSubitemYou need to change the data structure itself, but because the story is too complicated, it will be minimized.todoSubitemIt is not a stringyIf you think it is better to make it yourself, please try to log in to your app with the above code, including what you said.(About additional code part after editing)if NSUserDefaults.standardUserDefaults().objectForKey("todoList") != nil {
todoItem = NSUserDefaults.standardUserDefaults().objectForKey("todoList") as! [String]
}
if NSUserDefaults.standardUserDefaults().objectForKey("todoList1") != nil {
todoSubitem = NSUserDefaults.standardUserDefaults().objectForKey("todoList1") as! [String]
}
How to writeobjectForKey(_:)I would like to remember how to use if-let because it will be called twice, but it is not wrong.In the first half of this answer, I wrote "I need to modify the contents of todoSubitem so that the contents of todoItem are matched.""todoList"The data is"todoList1"If the data does not exist, it is said that it is the current state.todoItemHometodoSubitemIf the data is unmatched, it will not be processed correctly.Now, you’re testing in the simulator, so if you try to reset your content (Reset Contents and Settings...), you’ll be able to connect with “all” empty, but it will disappear from the data of other apps during development, and it’s hard to reset every bug, so add this code to the top line. //バグなどのせいで、todoItemとtodoSubitemに不整合が生じたら、両方リセットしてしまう
if todoItem.count != todoSubitem.count {
todoItem = []
todoSubitem = []
NSLog("todoItemとtodoSubitemに不整合が生じたので両方リセットしました")
}
Of course, while proceeding with the development, all the bugs are crushed and the aboveNSLogDon't leave the message.If you have any questions, please let us know in the.