2017-12-23 7 views
0

내 uitableview의 맨 위 행이 편집 가능하지 않은 이유는 모르지만 다른 모든 행은 정상적으로 작동하고 예상대로 삭제됩니다. caneditrowat indexPath와 같습니다. IndexPath는 그 한 행에 대해 작동하지 않습니다. 첨부 된 이미지를 참조하십시오.uitableview에서 상단 행을 편집/삭제할 수 없음 - Swift

내 tableview (_ : commit : forRowAt :)의 코드는 내가 찾을 수있는 모든 자습서와 비슷하지만이 문제가있는 다른 예제를 찾을 수없는 것 같습니다.

+1

유일한 가능한 방법 수도있을 =>있는 tableView FUNC (_있는 tableView : jQuery과, editingStyleForRowAt indexPath : IndexPath) -> UITableViewCellEditingStyle {indexPath.row == 0 {return UITableViewCellEditingStyle.none}이 (가)있는 경우 UITableViewCellEditingStyle.delete를 반환합니다. –

+0

대단히 감사합니다. @KumarReddy! if 문없이 솔루션을 사용하면 효과적입니다. –

+0

내 의견을 도움이 되십시오 :) 그리고 위로 투표 –

답변

1
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle 
    { 
     return UITableViewCellEditingStyle.delete 
    } 

대답 @ KumarReddy의 솔루션에서 적응 도움을

//MARK: Properties 
    var favouriteExercises = [FavouriteExercise]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    //Load exercises from local DB 
    if let savedFavouriteExercises = loadFavouriteExercises() 
    { 
     //loading exercises in from the favourites 
     favouriteExercises += savedFavouriteExercises 
    } 

    // Use the edit button item provided by the table view controller. 
    navigationItem.rightBarButtonItem = editButtonItem 
} 

override func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    //count number of rows in table 
    return favouriteExercises.count 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    // Table view cells are reused and should be dequeued using a cell identifier. 
    let cellIdentifier = "FavouriteTableViewCell" 

    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? FavouriteTableViewCell else { 
     fatalError("The dequeued cell is not an instance of FavouriteTableViewCell.") 
    } 

    // Fetches the appropriate exercise for the data source layout. 
    let exercise = favouriteExercises[indexPath.row] 

    //setup the layout for the cell in the table view 
    cell.nameLabel.text = exercise.name 
    let url = URL(string: (exercise.iconUrl))! 
    cell.photoImageView.sd_setImage(with: url) 
    //cell.photoImageView.image = #imageLiteral(resourceName: "defaultPhoto") 
    cell.backgroundColor = UIColor.darkGray 

    return cell 
} 

// Override to support conditional editing of the table view. 
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 
    // Return false if you do not want the specified item to be editable. 
    return true 
} 

// Override to support editing the table view. 
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == .delete { 
     // Delete the row from the data source 
     favouriteExercises.remove(at: indexPath.row) 
     saveFavouriteExercisess() 
     tableView.deleteRows(at: [indexPath], with: .fade) 
    } 
} 

감사