Swift에서 Int를 기반으로 UITableView의 특정 행 새로 고침
저는 Swift의 초보 개발자이며 UITableView를 포함하는 기본 앱을 만들고 있습니다. 다음을 사용하여 테이블의 특정 행을 새로 고치고 싶습니다.
self.tableView.reloadRowsAtIndexPaths(paths, withRowAnimation: UITableViewRowAnimation.none)
그리고 새로 고칠 행을 rowNumber라는 Int에서 가져오고 싶습니다.
문제는이 작업을 수행하는 방법을 모르고 검색 한 모든 스레드가 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 3.0에서
let rowNumber: Int = 2
let sectionNumber: Int = 0
let indexPath = IndexPath(item: rowNumber, section: sectionNumber)
self.tableView.reloadRows(at: [indexPath], with: .automatic)
기본적으로 TableView에 섹션이 하나만있는 경우 섹션 값 0을 입력 할 수 있습니다.
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)
}
어때 :
self.tableView.reloadRowsAtIndexPaths([NSIndexPath(rowNumber)], withRowAnimation: UITableViewRowAnimation.Top)
In addition, If you have sections for tableview, you should not try to find every rows you want to refresh, you should use reload sections. It is easy and more balanced process:
yourTableView.reloadSections(IndexSet, with: UITableViewRowAnimation)
Swift 4.1
use it when you delete row using selectedTag of row.
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)
I realize this question is for Swift, but here is the Xamarin equivalent code of the accepted answer if someone is interested.
var indexPath = NSIndexPath.FromRowSection(rowIndex, 0);
tableView.ReloadRows(new NSIndexPath[] { indexPath }, UITableViewRowAnimation.Top);
'Programing' 카테고리의 다른 글
| Visual Studio에서 현재 탭을 제외한 모든 탭을 닫는 바로 가기 키? (0) | 2020.10.23 |
|---|---|
| 다른 벡터가 아닌 한 벡터에 무엇이 있는지 어떻게 알 수 있습니까? (0) | 2020.10.23 |
| 사용자 지정 SSL 인증서 (AWS IAM에 저장 됨)를 선택할 수 없습니다. (0) | 2020.10.23 |
| 끝에서 시작까지 C ++ 벡터 반복 (0) | 2020.10.23 |
| WatiN 오류로 어셈블리를로드 할 수 없습니다. (0) | 2020.10.23 |