ableView。我目前正在使用此健身应用,希望在这里获得一些指导。
基本上,我想要的是能够滑动单元格并删除该行以及与该行关联的部分。
运行代码时,这是我得到的错误
“试图从第1部分中删除第0行,但更新前只有1个部分”
任何帮助将不胜感激。
class WorkoutsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, SwipeTableViewCellDelegate, CollapsibleTableSectionDelegate {
let realm = try! Realm()
var workouts : Results<Workouts>?
var days : Results<WeekDays>!
var daysOfWeek : [String] = ["Monday", "Tuesday", "Wednsday", "Thursday", "Friday", "Saturday", "Sunday"]
let picker = UIPickerView()
@IBOutlet weak var WorkoutsTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
WorkoutsTableView.delegate = self
WorkoutsTableView.dataSource = self
picker.delegate = self
picker.dataSource = self
loadCategories()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
tableView.rowHeight = 80.0
//Populate based on the # of workouts in each day.
let day = days[section]
return day.workouts.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return days[section].day
}
func numberOfSections(in tableView: UITableView) -> Int {
return days.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SwipeTableViewCell
cell.delegate = self
if (days?[indexPath.section]) != nil {
cell.accessoryType = .disclosureIndicator
//Populate with titles of workouts based on section/day of the week.
//cell.textLabel?.text = days?[indexPath.row].workouts[indexPath.row].name
cell.textLabel?.text = days[indexPath.section].workouts[indexPath.row].name
}
return cell
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
guard orientation == .right else { return nil }
let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
self.updateModel(at: indexPath)
}
// customize the action appearance
deleteAction.image = UIImage(named: "delete-icon")
return [deleteAction]
}
func tableView(_ tableView: UITableView, editActionsOptionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeOptions {
var options = SwipeOptions()
options.expansionStyle = .destructive
return options
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
@IBAction func AddWorkoutButton(_ sender: UIButton) {
var textField = UITextField()
textField.becomeFirstResponder()
let alert = UIAlertController(title: "New Workout", message: "Please name your workout...", preferredStyle: .alert)
let addAction = UIAlertAction(title: "Add Workout", style: .default) { (UIAlertAction) in
//Add workout to database
//Create a two dimensional array object in Realm with numbers corresponding to each day of the week.
//Append workouts to the day in the dictionary that the user selects.
let newWorkout = Workouts()
let dow = WeekDays()
dow.day = self.daysOfWeek[self.picker.selectedRow(inComponent: 0)]
newWorkout.name = textField.text!
dow.workouts.append(newWorkout)
self.save(newDay: dow)
}
alert.addTextField { (alertTextField) in
alertTextField.placeholder = "Muscle Group"
textField = alertTextField
alertTextField.inputView = self.picker
}
alert.addAction(addAction)
present(alert, animated: true, completion: nil)
}
func save(newDay: WeekDays){
do {
try realm.write {
realm.add(newDay)
}
} catch {
print("Error saving workout \(error)")
}
WorkoutsTableView.reloadData()
}
func updateModel(at indexPath: IndexPath){
if let workoutForDeletion = self.days?[indexPath.section]{
do {
try self.realm.write {
self.realm.delete(workoutForDeletion)
}
} catch {
print("Error deleting workout, \(error)")
}
}
self.WorkoutsTableView.reloadData()
}
func loadCategories(){
days = realm.objects(WeekDays.self)
WorkoutsTableView.reloadData()
}
@IBAction func EditWorkout(_ sender: UIBarButtonItem) {
}
}
extension WorkoutsViewController : UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 7
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return daysOfWeek[row]
}
}
class WeekDays : Object {
@objc dynamic var day : String = ""
let workouts = List<Workouts>()
}
class Workouts : Object {
@objc dynamic var name : String = ""
var parentDay = LinkingObjects(fromType: WeekDays.self, property: "workouts")
}
答案 0 :(得分:1)
如果您要删除整个部分,则您的代码是正确的。尝试调用loadCategories而不是重新加载tableView来查看是否有帮助。查看下面的更新代码:
改为将该行更改为此:
SELECT t1.*, t2.[2017], t2.[2018], t2.[2019]
FROM
(
SELECT
OINV.DocEntry,
OINV.DocNum AS N'FV',
OITM.ItemCode,
OITM.ItemName,
CAST(T1.Ilosc AS int) AS N'Sum',
T1.unitMsr AS N'UoM',
T1.[Price Min],
T1.Currency AS N'PLN',
T1.DocDate AS N'Last'
FROM
OITM
INNER JOIN
(
SELECT
OINV.CardCode,
MAX(OINV.DocDate) AS DocDate,
MAX(OINV.DocEntry) AS DocEntry,
INV1.ItemCode,
SUM(INV1.Quantity) AS Ilosc,
INV1.unitMsr,
MIN(INV1.Price) AS 'Price Min',
INV1.Currency
FROM OINV inner join INV1 ON OINV.DocEntry = INV1.DocEntry
WHERE INV1.ItemCode is not null
GROUP BY OINV.CardCode, INV1.ItemCode, INV1.unitMsr, INV1.Currency
) AS T1 ON OITM.ItemCode = T1.ItemCode
INNER JOIN OINV ON OINV.DocEntry = T1.DocEntry
WHERE T1.CardCode = N'OT-05453' AND T1.ItemCode NOT IN (SELECT DISTINCT U_ItemCode FROM [@BP2] WHERE U_CardCode = N'OT-05453' )) t1
INNER JOIN
(SELECT
P.ItemCode,
[2017] [2017],
[2018] [2018],
[2019] [2019]
FROM
(SELECT
INV1.ItemCode,
OINV.CardCode as C,
INV1.Quantity Volume,
year(INV1.[DocDate]) as year
FROM INV1 INNER JOIN OINV ON INV1.docentry = OINV.docentry
WHERE INV1.[docDate] between year(2007) and GETDATE() AND OINV.cardcode = N'OT-05453'
) S
Pivot
(sum([volume]) For year IN ([2017],[2018],[2019])) P) t2
on t1.ItemCode = t2.ItemCode