programing

IntIn Swift를 기준으로 UITableView의 특정 행 새로 고침

muds 2023. 8. 14. 23:11
반응형

IntIn Swift를 기준으로 UITableView의 특정 행 새로 고침

저는 스위프트의 초보 개발자이며, UITableView가 포함된 기본 앱을 만들고 있습니다.다음을 사용하여 표의 특정 행을 새로 고칩니다.

self.tableView.reloadRowsAtIndexPaths(paths, withRowAnimation: UITableViewRowAnimation.none)

그리고 이름이 지정된 행 번호에서 새로 고쳐질 행을 원합니다.

문제는 제가 이걸 어떻게 하는지도 모르고 제가 검색한 모든 스레드가 Obj-C를 위한 것이라는 것입니다.

아이디어 있어요?

다음을 생성할 수 있습니다.NSIndexPath행과 섹션 번호를 사용하여 다음과 같이 다시 로드합니다.

let indexPath = NSIndexPath(forRow: rowNumber, inSection: 0)
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)

이 예제에서는 테이블에 섹션이 하나(예: 0)만 있다고 가정했지만 그에 따라 값을 변경할 수 있습니다.

Swift 3.0용 업데이트:

let indexPath = IndexPath(item: rowNumber, section: 0)
tableView.reloadRows(at: [indexPath], with: .top)

소프트 임팩트 애니메이션 솔루션의 경우:

스위프트 3:

let indexPath = IndexPath(item: row, section: 0)
tableView.reloadRows(at: [indexPath], with: .fade)

Swift 2.x:

let indexPath = NSIndexPath(forRow: row, inSection: 0)
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

이는 앱이 손상되지 않도록 보호하는 또 다른 방법입니다.

스위프트 3:

let indexPath = IndexPath(item: row, section: 0)
if let visibleIndexPaths = tableView.indexPathsForVisibleRows?.index(of: indexPath as IndexPath) {
    if visibleIndexPaths != NSNotFound {
        tableView.reloadRows(at: [indexPath], with: .fade)
    }
}

Swift 2.x:

let indexPath = NSIndexPath(forRow: row, inSection: 0)
if let visibleIndexPaths = tableView.indexPathsForVisibleRows?.indexOf(indexPath) {
   if visibleIndexPaths != NSNotFound {
      tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
   }
}

스위프트 4

let indexPathRow:Int = 0    
let indexPosition = IndexPath(row: indexPathRow, section: 0)
tableView.reloadRows(at: [indexPosition], with: .none)

SWIFT 4.2

    func reloadYourRows(name: <anyname>) {
    let row = <your array name>.index(of: <name passing in>)
    let reloadPath = IndexPath(row: row!, section: 0)
    tableView.reloadRows(at: [reloadPath], with: .middle)
    }

Swift 3.0에서

let rowNumber: Int = 2
let sectionNumber: Int = 0

let indexPath = IndexPath(item: rowNumber, section: sectionNumber)

self.tableView.reloadRows(at: [indexPath], with: .automatic)

기본적으로 테이블 뷰에 섹션이 하나만 있는 경우 섹션 값 0을 입력할 수 있습니다.

let indexPathRow:Int = 0
let indexPosition = IndexPath(row: indexPathRow, section: 0)
tableView.reloadRows(at: [indexPosition], with: .none)

이 질문이 Swift를 위한 것이라는 것을 알지만, 여기 누군가 관심이 있다면 수락된 답변의 사마린 등가 코드가 있습니다.

var indexPath = NSIndexPath.FromRowSection(rowIndex, 0);
tableView.ReloadRows(new NSIndexPath[] { indexPath }, UITableViewRowAnimation.Top);

또한 테이블 뷰에 대한 섹션이 있는 경우 새로 고칠 모든 행을 찾으려고 하지 말고 다시 로드 섹션을 사용해야 합니다.쉽고 균형 잡힌 프로세스입니다.

yourTableView.reloadSections(IndexSet, with: UITableViewRowAnimation)
    extension UITableView {
        /// Reloads a table view without losing track of what was selected.
        func reloadDataSavingSelections() {
            let selectedRows = indexPathsForSelectedRows

            reloadData()

            if let selectedRow = selectedRows {
                for indexPath in selectedRow {
                    selectRow(at: indexPath, animated: false, scrollPosition: .none)
                }
            }
        }
    }

tableView.reloadDataSavingSelections()

어때요?

self.tableView.reloadRowsAtIndexPaths([NSIndexPath(rowNumber)], withRowAnimation: UITableViewRowAnimation.Top)

스위프트 4.1

선택한 행 태그를 사용하여 행을 삭제할 때 사용합니다.

self.tableView.beginUpdates()

        self.yourArray.remove(at:  self.selectedTag)
        print(self.allGroups)

        let indexPath = NSIndexPath.init(row:  self.selectedTag, section: 0)

        self.tableView.deleteRows(at: [indexPath as IndexPath], with: .automatic)

        self.tableView.endUpdates()

        self.tableView.reloadRows(at: self.tableView.indexPathsForVisibleRows!, with: .automatic)

언급URL : https://stackoverflow.com/questions/28206492/refresh-certain-row-of-uitableview-based-on-int-in-swift

반응형