Reputation: 123
I want to make a repeat list like in the picture below for my alarm app in swift, I've tried my best but all my attempts have failed, how I can do it, how I can save multiple values in one attributes in database, do I need array, how i can do check list; my project is almost complete, the only thing left is repeat list which I failed to make it
thanks in advance
Reminder.swift
verride func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "presentDay" {
if let daysPickerVie
wController = segue.destinationViewController as? Days {
let path = tableView.indexPathForSelectedRow
detailLabel.text = daysPickerViewController.days[(path?.row)!]
}
}
}
Days.swift
class Days: UITableViewController
{
var lastSelectedIndexPath = NSIndexPath(forRow: -1, inSection: 0)
let days = [
"Everyday",
"Saturday",
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday"]
var selectedDay:String?
var selectedDayIndex:Int?
var cell:UITableViewCell?
override func viewDidLoad() {
//navigationController?.navigationBar.topItem?.title = "Back"
}
extension Days{
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return days.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("daySelected", forIndexPath: indexPath) as UITableViewCell
cell.accessoryType = .None
cell.textLabel?.text = days[indexPath.row]
selectedDay = days[indexPath.row]
return cell
}
}
extension Days {
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
if cell.selected {
cell.accessoryType = .Checkmark
}
}
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
cell.accessoryType = .None
}
}
}
Upvotes: 1
Views: 770
Reputation: 434
Answer for User question about Mundi Answer for future reference: You don't need a switch. Just parse the data as binary: so For example 3 in binary is 11 -> the first one is in the 2s case and the second in the 1s case. So it the last digit of the binary number is 1 then Sunday is selected. If the second from right is 1 then Tuesday is selected and so on.
Example
14
In binary: 1110
1st case from right is 0 -> Sunday is not selected
2nd case from right is 1 -> Monday is selected
3rd case from right is 1 -> Tuesday is selected
4th case from right is 1 -> Wednesday is selected
There are no more cases so no other day is selected
Upvotes: 0
Reputation: 135
I was having same Problem but solve it.There is no need of "didDeselectRowAtIndexPath" method. Update your code like below:
var selectedIndexPaths = [IndexPath]()
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let day = days[(indexPath as NSIndexPath).row]
cell.textLabel?.text = day
if self.selectedIndexPaths.contains(indexPath) {
cell.accessoryType = .checkmark
}
else {
cell.accessoryType = .none
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
if self.selectedIndexPaths.contains(indexPath) {
self.selectedIndexPaths.remove(at: self.selectedIndexPaths.index(of: indexPath)!)
cell?.accessoryType = .none
}
else {
cell?.accessoryType = .checkmark
self.selectedIndexPaths.append(indexPath)
}
}
Upvotes: 0
Reputation: 80273
I think the most appropriate method here is the good old bit mask. Recognizing that bitwise operations are difficult for many people[1] please find below a solution that works without bitwise operators. The attribute can be of type Int
and it can capture all possible variations with the following logic:
0 no days selected
1 Sundays selected
2 Mondays selected
4 Tuesdays selected
8 Wednesdays selected
// etc...
The value to be stored is the sum of all selections, so e.g. for every Monday and Friday it would be 2 + 32 = 34
.
You can write an elegant struct
to handle the conversion from the raw value to the list of selected days.
[1] There are 10 kinds of people in the world. Those that understand binary numbers and those that don't.
Upvotes: 1