Automatically Reload Tableviewcontroller on Rewind

swift auto refresh Cell

If I understand you correctly, you can use reactive programming for this. For example Bond framework can be used like this:

let todoItems: SafeSignal<[TodoItem]> = ....
let tableView: UITableView = ...
class TodoItemCell: UITableView Cell { ... }

...

todoItems.bind(to: tableView, cellType: TodoItemCell.self) { (cell, item) in
cell.titleLabel.text = item.name
}

Using this framework, your table view will automatically reload when there are any changes to the source array. You will find more about usage on table views at this link.

How to use pull to refresh in Swift?

Pull to refresh is built in iOS. You could do this in swift like

let refreshControl = UIRefreshControl()

override func viewDidLoad() {
super.viewDidLoad()

refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
refreshControl.addTarget(self, action: #selector(self.refresh(_:)), for: .valueChanged)
tableView.addSubview(refreshControl) // not required when using UITableViewController
}

@objc func refresh(_ sender: AnyObject) {
// Code to refresh table view
}

At some point you could end refreshing.

refreshControl.endRefreshing()

unwinding segue, need to refresh tableview

When your controller pops off the stack, the underlying view controller will have its viewDidAppear(animated:) function called. So you could have:

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
tableView.reloadData()
}

This is assuming based on your post that the issue is only about reloading your table. If you reload the table and your cells are still not updating properly, you have an issue with your dataSource not configuring the cells properly. But I cannot tell if this is the case without any code.

Edit: this was a cell reuse issue. Cell configuation had some code to remove a subview in some instances, but never add the subview back if it was supposed to be there. The simplest fix is to use isHidden instead of outright removing the subview.

How do I get NSTimer to restart updating a label when returning to a ViewController?

The issue is that you are performing new segue to your timer view controller. This creates a new instance of your timer view controller rather than segueing back to the original one.

First delete the segue you have setup from your second to first controller. You will then need to use an unwind segue, you can learn how to do this by following the link below. (I had the same issue as yourself recently and this is what I was provided also)

https://developer.apple.com/library/ios/technotes/tn2298/_index.html

By using the unwind segue you shouldn't need to add any additional code as the label on your original controller should still be getting updated.

NSFetchedResultsController object deleted from UITableView on update

It appears my scouring of SO was not very good. see here https://stackoverflow.com/a/18998335/3482632

I added a UISegmentControl to filter and used the int value of its segment index.

 int index = self.filterSegment.selectedSegmentIndex;
NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"filter == %d", index]];
[fetchRequest setPredicate:predicate];

"filter" in my NSManagedObject subclass is an NSNumber. when I changed the NSPredicate to

 NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"filter == %@", @(index)]];

everything works fine.

Drag & Drop from TableView to Another View in Swift

Currently I does't have the time to test the code, but it should be enough to make sense.... You can do something like this:

class ViewController: UIViewController, UITableViewDataSource {

private var dragView: UIView?
@IBOutlet weak var dropZone: UIView!

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

let lpGestureRecognizer: UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(didLongPressCell))
cell.contentView.addGestureRecognizer(lpGestureRecognizer)

return cell
}

func didLongPressCell (recognizer: UILongPressGestureRecognizer) {
switch recognizer.state {
case .Began:
if let cellView: UIView = recognizer.view {
cellView.frame.origin = CGPointZero
dragView = cellView
view.addSubview(dragView!)
}
case .Changed:
dragView?.center = recognizer.locationInView(view)
case .Ended:
if (dragView == nil) {return}

if (CGRectIntersectsRect(dragView!.frame, dropZone.frame)) {
if let cellView: UIView = (dragView?.subviews[0])! as UIView {
cellView.frame.origin = CGPointZero
dropZone.addSubview(cellView)
}

dragView?.removeFromSuperview()
dragView = nil

//Delete row from UITableView if needed...
} else {
//DragView was not dropped in dropszone... Rewind animation...
}
default:
print("Any other action?")
}
}

}

Update on comment:

sure, one possibility would be to tag the fields... like this:

private let imageViewTag: Int = 997
private let textLabelTag: Int = 998
private let detailTtextLabelTag: Int = 999

//...

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

//...
cell.imageView?.tag = imageViewTag
cell.textLabel?.tag = textLabelTag
cell.detailTextLabel?.tag = detailTtextLabelTag
//...

}

func didLongPressCell (recognizer: UILongPressGestureRecognizer) {
//...
case .Ended:
let cellImageView: UIImageView? = recognizer.view?.viewWithTag(imageViewTag) as? UIImageView
let cellTextLabel: UITextField? = recognizer.view?.viewWithTag(textLabelTag) as? UITextField
let cellDetailTextLabel: UITextField? = recognizer.view?.viewWithTag(detailTtextLabelTag) as? UITextField
//...
}


Related Topics



Leave a reply



Submit