Refresh certain row of UITableView based on Int in Swift
You can create an NSIndexPath
using the row and section number then reload it like so:
let indexPath = NSIndexPath(forRow: rowNumber, inSection: 0)
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)
In this example, I've assumed that your table only has one section (i.e. 0) but you may change that value accordingly.
Update for Swift 3.0:
let indexPath = IndexPath(item: rowNumber, section: 0)
tableView.reloadRows(at: [indexPath], with: .top)
How to reload and animate just one UITableView cell/row?
Use the following UITableView
instance method:
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
You have to specify an NSArray
of NSIndexPaths
that you want to reload. If you just want to reload. If you only want to reload one cell, then you can supply an NSArray
that only holds one NSIndexPath
. For example:
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:3 inSection:0];
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[myUITableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
You can see the UITableViewRowAnimation
enum for all the possible ways of animating the row refresh. If you don't want any animation then you can use the value UITableViewRowAnimationNone
, as in the example.
Reloading specific rows has a greater advantage than simply getting the animation effect that you'd like. You also get a huge performance boost because only the cells that you really need to be reloaded are have their data refreshed, repositioned and redrawn. Depending on the complexity of your cells, there can be quite an overhead each time you refresh a cell, so narrowing down the amount of refreshes you make is a necessary optimization that you should use wherever possible.
How to Refresh particular cell in tableview and store that changed data to show later on same tableview
You need to maintain the state of your UITableViewCell
in your tableView's
dataSource
model. I'll try to elaborate that through an example.
1. Let's assume you model
looks something like:
class Post {
var isLiked = false
var likeCount = 0
}
2. Next, you need to create the custom UITableViewCell
such that it modifies the dataSource
and the UI
as per the button
action, i.e.
class CustomCell: UITableViewCell {
@IBOutlet weak var countLabel: UILabel!
@IBOutlet weak var likeButton: UIButton!
var post: Post?
func configure(with post: Post) {
countLabel.text = "\(post)"
likeButton.isSelected = post.isLiked
}
@IBAction func likeButtonTapped(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
if let post = post {
post.isLiked = sender.isSelected
if sender.isSelected {
post.likeCount += 1
} else {
post.likeCount -= 1
}
countLabel.text = "\(post.likeCount)"
}
}
}
3. Finally, the UITableViewDataSource
methods will be,
class VC: UIViewController, UITableViewDataSource {
var posts = [Post]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
cell.configure(with: posts[indexPath.row])
return cell
}
}
In the above code, whenever the likeButton
is pressed, the UI
and the dataSource
will be updated. So, whenever the cell
is displayed again, you'll see the last updated data automatically.
There is no need to reload the tableView
or cell
everytime the likeButton
is tapped. Reloading will only result in the extra overhead.
Swift: How to reload new content (eg. array) every time the tableview appears?
Your reloadData()
call is before updating array.
It should be after updating the datasource:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
weightArray = UserDefaults.standard.array(forKey: "favorites") as! [String]
tableView.reloadData()
}
Anyways, you can make use of the didSet
property observer of your datasource object.
//your weightArray declaration
var weightArray = [String]() {
didSet {
tableView.reloadData()
}
}
and then you can do:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
weightArray = UserDefaults.standard.array(forKey: "favorites") as! [String]
}
This way, anytime you change the array, your tableView
will reload.
Swift UITableView messing up after reloadData()
This is because of the reuse of cells.
Set the default color in else condition.
} else {
cell.cost.textColor = UIColor.gray
cell.cost.text = "\(items[indexPath.row].XP) XP"
}
Another way, you can set the default style property inside the prepareForReuse
method of UITableViewCell
class TableViewCell: UITableViewCell {
override func prepareForReuse() {
super.prepareForReuse()
// Set default cell style
}
}
Related Topics
How to Count Rows in a Parse Class Programmatically in an iOS App Using Swift
Pass Variables to a New View Controller via a Subclass and Dozens of Map Pins
Over the Air (Ota) iOS IPA File Distribution for Public
Macos on Vmware Doesn't Recognize iOS Device
iOS 8 - Screen Blank After Dismissing View Controller with Custom Presentation
Change Color of Png in Buttons - iOS
Afnetworking 2.0 Track File Upload Progress
How to Turn Flashlight on and Off in Swift
Xcode Instruments Allocations: Look at Live Bytes or Overall Bytes
How to Detect Tableview Cell Touched or Clicked in Swift
How to Use Multi-Path Update with the Rest API in Firebase? Error 400
How to Split a Numeric String Using Multiple Separators in a Swift Closure
Install .Ipa to iPad with or Without Itunes
Width and Height Equal to Its Superview Using Autolayout Programmatically
How to Ask User for Camera Access After They Have Already Denied It on iOS
Xcode 8 Beta - Convert to Current Swift Syntax Failed: Could Not Find Test Host