Objective C - UICollectionViewListCell Add swipe to delete - objective-c

Trying to implement "Swipe to Delete" API for UICollectionViewListCell.
I'm writing in Objective-C
the compiler is not auto-completing the code.
Any reasons? example code?
Swift example:
let listConfig = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
listConfig.trailingSwipeActionsConfigurationProvider = { [weak self] indexPath in
guard let self = self else { return nil }
let action = UIContextualAction(style: .normal, title: "Done!", handler: actionHandler)
return UISwipeActionsConfiguration(actions: [action])
}
Any code example for Objective C?
trying to reach the following result:

UICollectionLayoutListConfiguration * listConfiguration = [[UICollectionLayoutListConfiguration alloc]initWithAppearance:UICollectionLayoutListAppearanceInsetGrouped];
[listConfiguration setTrailingSwipeActionsConfigurationProvider:^UISwipeActionsConfiguration* (NSIndexPath *indexPath) {
UIContextualAction *action = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:[NSLocalizedString(#"Delete", nil)capitalizedString] handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
}];
return [UISwipeActionsConfiguration configurationWithActions:#[action]];
}];

Related

How to use requestGeometryUpdateWithPreferences in Objective C

I have an example in Swift language:
guard let windowScene = view.window?.windowScene else { return }
windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: .portrait)) { error in }
I can't write it in Objective C:
UIWindowScene *windowScene = self.view.window.windowScene;
[windowScene requestGeometryUpdateWithPreferences: UIInterfaceOrientationMaskPortrait errorHandler:nil];
Please tell me how to write correctly I will be grateful for any help.
One way to write that Swift code in Objective-C would be:
UIWindowScene *windowScene = self.view.window.windowScene;
if (!windowScene) { return; }
UIWindowSceneGeometryPreferences *preferences = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:UIInterfaceOrientationMaskPortrait];
[windowScene requestGeometryUpdateWithPreferences:preferences errorHandler:^(NSError * _Nonnull error) {
// Handle error here
}];

UIContextMenuConfiguration in objc

I would like to implement the UIContextMenuConfiguration in objC
There are many examples in swift , but I have to close a matter in objc ...
So in swift I found examples like this
override func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { actions -> UIMenu<UIAction>? in
let action = UIAction(__title: "Custom action", image: nil, options: []) { action in
// Put button handler here
}
return UIMenu<UIAction>.create(title: "Menu", children: [action])
}
return configuration
}
In objc I can not define the actions
I can only produce mistakes ...
this is my example code...
- (UIContextMenuConfiguration *)tableView:(UITableView *)tableView
contextMenuConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath
point:(CGPoint)point API_AVAILABLE(ios(13.0)){
BlogPost *blogPost = [self.blogPosts objectAtIndex:indexPath.row];
UIAction * lettura = [UIAction actionWithTitle:#"Leggi"
image:nil
identifier:nil
handler:^(UIAction *action){[self presentSF:indexPath];}
];
UIMenu * menu = [UIMenu menuWithTitle:#"" children:#[lettura]];
UIContextMenuConfiguration * config = [UIContextMenuConfiguration configurationWithIdentifier:nil
previewProvider:^ UIViewController* {
SFSafariViewController *previewSFController = [[SFSafariViewController alloc] initWithURL:blogPost.url entersReaderIfAvailable:NO];
previewSFController.preferredControlTintColor=[UIColor blackColor];
previewSFController.delegate = self;
return previewSFController;
}
actionProvider:nil];
return config;
}
Someone can help me
Thank you so much in advance for your help
Vanni
Sorry you people
I solved ...
I just had to pass an array...
actionProvider:^(NSArray* suggestedAction){return menu;}
- (UIContextMenuConfiguration*)tableView:(UITableView*)tableView contextMenuConfigurationForRowAtIndexPath:(NSIndexPath*)indexPath point:(CGPoint)point
{
UIContextMenuConfiguration* config = [UIContextMenuConfiguration configurationWithIdentifier:nil
previewProvider:nil
actionProvider:^UIMenu* _Nullable(NSArray<UIMenuElement*>* _Nonnull suggestedActions) {
NSMutableArray* actions = [[NSMutableArray alloc] init];
//Your Action
[actions addObject:[UIAction actionWithTitle:#"Favoritar!" image:[UIImage systemImageNamed:#"star"] identifier:nil handler:^(__kindof UIAction* _Nonnull action) {
[self updateFavoriteTournament:self.tournamentArray[indexPath.row][#"_id"]];
}]];
UIMenu* menu = [UIMenu menuWithTitle:#"" children:actions];
return menu;
}];
return config;
}

Touch Slide over UIButton issue

Can someone tell me if I have translated the first 2 lines correctly to Swift and if the first part is correctly? Also, could anyone help me figure out the rest. I can't figure out how to translate the if statement at the bottom..
[C addTarget:self action:#selector(outsideOfKey: forEvent:) forControlEvents:UIControlEventTouchDragOutside|UIControlEventTouchDragInside];
[C addTarget:self action:#selector(keyGetsLeft: forEvent:) forControlEvents:UIControlEventTouchUpOutside | UIControlEventTouchUpInside];
-(void) outsideOfKey:(id)sender forEvent:(UIEvent *)event
{
for(UITouch *t in [event allTouches])
{
CGPoint touchPoint = [t locationInView:window];
if(CGRectContainsPoint(C.frame, touchPoint))
{
C.highlighted = YES;
}
else{
C.highlighted = NO;
}
Translated to swift
C.addTarget(self, action:Selector("outsideOfKey:forEvent:"), forControlEvents:.TouchDragOutside)
C.addTarget(self, action:Selector("outsideOfKey:forEvent:"), forControlEvents:.TouchDragInside)
C.addTarget(self, action:Selector("keyGetsLeft:forEvent:"), forControlEvents:.TouchUpOutside)
C.addTarget(self, action:Selector("keyGetsLeft:forEvent:"), forControlEvents:.TouchUpInside)
func outsideOfKey (sender: AnyObject, forEvent: UIEvent) {
let touch = event.allTouches() as? UITouch
for touch
{
var touchPoint : CGPoint = touch.locationInView(window)
if(CGRectContainsPoint(C.frame, touchPoint))
{
C.highlighted = YES;
}
else{
C.highlighted = NO;
}
}
Try something like:
C.addTarget(self, action:Selector("outsideOfKey:forEvent:"), forControlEvents:.TouchDragOutside | .TouchDragInside)
C.addTarget(self, action:Selector("keyGetsLeft:forEvent:"), forControlEvents:.TouchUpOutside | .TouchUpInside)
func outsideOfKey(sender: AnyObject, forEvent event: UIEvent) {
if let touches = event.allTouches()?.allObjects as? [UITouch] {
for touch in touches {
var touchPoint : CGPoint = touch.locationInView(window)
if CGRectContainsPoint(C.frame, touchPoint) == true {
C.highlighted = true;
} else {
C.highlighted = false;
}
}
}
}
Points:
You can still use the "|" operator on enums in swift (most of the time)
You don't put parentheses around the conditional clause in swift
YES and NO are not valid in swift, you much use true and false
Swift does not support sets so it is easiest to get the touches as an Array so you can iterate using a simple swift for...in loop
The "if let ..." pattern is called optional binding, if the right hand side is not nil "touches" will be set to the value and the code in the following braces will be executed, otherwise it will skip the block of code
The way "?" is used after ".allTouches()" is called optional chaining, if .allTouches() returns nil, the whole expression will return nil
Hope this is of some use to you, let me know if you have any more swift queries!

What have I got wrong with these code blocks?

I'm attempting to use Tony Million's Reachability within a new Swift based app. I have it implemented in another app I wrote in Obj C, but I'm having issues with getting the proper syntax in Swift. The code blocks are as follows:
override func viewDidLoad() {
super.viewDidLoad()
messageText.text = ""
var reach: Reachability = Reachability(hostName: "www.apple.com")
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged", name: kReachabilityChangedNotification, object: nil)
reach.reachableBlock = Reachability()
{
dispatch_async(dispatch_get_main_queue(), {
self.messageText.text = "Enter search criteria...";
})
}
reach.unreachableBlock = Reachability()
{
dispatch_async(dispatch_get_main_queue(), {
self.messageText.text = "Attempting to contact network...";
})
}
reach.startNotifier()
}
AND
func reachabilityChanged(note: NSNotification)
{
var reach: Reachability = Reachability()
if(reach.isReachable())
{
messageText.text = "Enter search criteria...";
}
else
{
messageText.text = "Attempting to contact network...";
}
}
My issues are first, my "blocks" for "reachable" and "unreachable" are not the correct syntax and I'm at a loss for what is the proper syntax for these blocks. My second issue is with the "reachabilityChanged" function. I get an error stating "-[_TtC9icdDRPlus20SearchViewController reachabilityChanged]: unrecognized selector sent to instance" which I'm again at a loss. Condsider my Obj C code as follows:
-(void)viewDidLoad
{
[super viewDidLoad];
NSString *popUpShownOnce = #"YES";
NSInteger swipeCount = 0;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:popUpShownOnce forKey:#"popDisplayed"];
[defaults setInteger:swipeCount forKey:#"showswipearrows"];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
[[UIApplication sharedApplication] setStatusBarHidden:NO];
Reachability * reach = [Reachability reachabilityWithHostname:#"somesite.com"];
reach.reachableBlock = ^(Reachability * reachability)
{
dispatch_async(dispatch_get_main_queue(), ^{
searchForText.placeholder = #"Enter search criteria...";
});
};
reach.unreachableBlock = ^(Reachability * reachability)
{
dispatch_async(dispatch_get_main_queue(), ^{
searchForText.placeholder = #"Attempting to contact network...";
});
};
[reach startNotifier];
}
-(void)reachabilityChanged:(NSNotification*)note
{
Reachability * reach = [note object];
if([reach isReachable])
{
searchForText.placeholder = #"Enter search criteria...";
}
else
{
searchForText.placeholder = #"Attempting to contact network...";
}
}
Assistance is greatly appreciated. The questions are as follows:
1. What is the proper translation for the reachable and unreachable blocks from Obj C to Swift?
2. If my addObserver call is correct, why might I get the unrecognized selector error? If not correct, what is the proper call?
Thanks in advance.
The correct syntax for closures is { (<params>) -> <return type> in <statements> }:
reach.reachableBlock = { (reachability) in
dispatch_async(dispatch_get_main_queue(), {
self.messageText.text = "Enter search criteria...";
})
}
With NSNotificationCenter, looks like you missed the colon in the selector argument:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: kReachabilityChangedNotification, object: nil)
Your error after making the changes that #Austin recommended is that self.reachabilityRef in SCNetworkReachabilitySetCallback is NULL.
try:
func reachabilityChanged(note: NSNotification!) {
var reach: Reachability! = Reachability(reachabilityRef: note.object as SCNetworkReachability)
if(reach.isReachable()) {
messageText.text = "Enter search criteria...";
} else {
messageText.text = "Attempting to contact network...";
}
}
UPDATE:
It would seem that SCNetworkReachability is not currently fully working with Swift: according to this: https://twitter.com/marksands/status/474717606004273152
I got it to work like this:
var reachability: Reachability?
override func viewDidLoad() {
super.viewDidLoad()
// Setup reachability
reachability = Reachability(hostName: "www.google.com")
reachability!.reachableBlock = { (reach) in
dispatch_async(dispatch_get_main_queue(), {
self.titleLabel.attributedText = Utilities.myAttributedText("Online", mySize: 18, myFont: "HelveticaNeue", myColor: UIColor.whiteColor())
self.titleLabel.sizeToFit()
})
}
reachability!.unreachableBlock = { (reach) in
dispatch_async(dispatch_get_main_queue(), {
self.titleLabel.attributedText = Utilities.myAttributedText("Offline", mySize: 18, myFont: "HelveticaNeue", myColor: UIColor.whiteColor())
self.titleLabel.sizeToFit()
})
}
reachability!.startNotifier()
}
this worked for me:
//Reachability
myReachabilityInstance = Reachability(hostName: "www.google.com")
myReachabilityInstance?.reachableOnWWAN = false
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityDidChangeMethod", name: kReachabilityChangedNotification, object: nil)
myReachabilityInstance?.startNotifier()

UICollectionView Performing Updates using performBatchUpdates

I have a UICollectionView which I am trying to insert items into it dynamically/with animation. So I have some function that downloads images asynchronously and would like to insert the items in batches.
Once I have my data, I would like to do the following:
[self.collectionView performBatchUpdates:^{
for (UIImage *image in images) {
[self.collectionView insertItemsAtIndexPaths:****]
}
} completion:nil];
Now in place of the ***, I should be passing an array of NSIndexPaths, which should point to the location of the new items to be inserted. I am very confused since after providing the location, how do I provide the actual image that should be displayed at that position?
Thank you
UPDATE:
resultsSize contains the size of the data source array, self.results, before new data is added from the data at newImages.
[self.collectionView performBatchUpdates:^{
int resultsSize = [self.results count];
[self.results addObjectsFromArray:newImages];
NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];
for (int i = resultsSize; i < resultsSize + newImages.count; i++)
[arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
[self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths];
} completion:nil];
See Inserting, Deleting, and Moving Sections and Items from the "Collection View Programming Guide for iOS":
To insert, delete, or move a single section or item, you must follow
these steps:
Update the data in your data source object.
Call the appropriate method of the collection view to insert or delete the section or item.
It is critical that you update your data source before notifying the
collection view of any changes. The collection view methods assume
that your data source contains the currently correct data. If it does
not, the collection view might receive the wrong set of items from
your data source or ask for items that are not there and crash your
app.
So in your case, you must add an image to the collection view data source first and then call insertItemsAtIndexPaths. The collection view will then ask the data source delegate function to provide the view for the inserted item.
I just implemented that with Swift. So I would like to share my implementation.
First initialise an array of NSBlockOperations:
var blockOperations: [NSBlockOperation] = []
In controller will change, re-init the array:
func controllerWillChangeContent(controller: NSFetchedResultsController) {
blockOperations.removeAll(keepCapacity: false)
}
In the did change object method:
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
if type == NSFetchedResultsChangeType.Insert {
println("Insert Object: \(newIndexPath)")
blockOperations.append(
NSBlockOperation(block: { [weak self] in
if let this = self {
this.collectionView!.insertItemsAtIndexPaths([newIndexPath!])
}
})
)
}
else if type == NSFetchedResultsChangeType.Update {
println("Update Object: \(indexPath)")
blockOperations.append(
NSBlockOperation(block: { [weak self] in
if let this = self {
this.collectionView!.reloadItemsAtIndexPaths([indexPath!])
}
})
)
}
else if type == NSFetchedResultsChangeType.Move {
println("Move Object: \(indexPath)")
blockOperations.append(
NSBlockOperation(block: { [weak self] in
if let this = self {
this.collectionView!.moveItemAtIndexPath(indexPath!, toIndexPath: newIndexPath!)
}
})
)
}
else if type == NSFetchedResultsChangeType.Delete {
println("Delete Object: \(indexPath)")
blockOperations.append(
NSBlockOperation(block: { [weak self] in
if let this = self {
this.collectionView!.deleteItemsAtIndexPaths([indexPath!])
}
})
)
}
}
In the did change section method:
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
if type == NSFetchedResultsChangeType.Insert {
println("Insert Section: \(sectionIndex)")
blockOperations.append(
NSBlockOperation(block: { [weak self] in
if let this = self {
this.collectionView!.insertSections(NSIndexSet(index: sectionIndex))
}
})
)
}
else if type == NSFetchedResultsChangeType.Update {
println("Update Section: \(sectionIndex)")
blockOperations.append(
NSBlockOperation(block: { [weak self] in
if let this = self {
this.collectionView!.reloadSections(NSIndexSet(index: sectionIndex))
}
})
)
}
else if type == NSFetchedResultsChangeType.Delete {
println("Delete Section: \(sectionIndex)")
blockOperations.append(
NSBlockOperation(block: { [weak self] in
if let this = self {
this.collectionView!.deleteSections(NSIndexSet(index: sectionIndex))
}
})
)
}
}
And finally, in the did controller did change content method:
func controllerDidChangeContent(controller: NSFetchedResultsController) {
collectionView!.performBatchUpdates({ () -> Void in
for operation: NSBlockOperation in self.blockOperations {
operation.start()
}
}, completion: { (finished) -> Void in
self.blockOperations.removeAll(keepCapacity: false)
})
}
I personally added some code in the deinit method as well, in order to cancel the operations when the ViewController is about to get deallocated:
deinit {
// Cancel all block operations when VC deallocates
for operation: NSBlockOperation in blockOperations {
operation.cancel()
}
blockOperations.removeAll(keepCapacity: false)
}
I was facing the similar issue while deleting the item from index and this is what i think we need to do while using performBatchUpdates: method.
1# first call deleteItemAtIndexPath to delete the item from collection view.
2# Delete the element from array.
3# Update collection view by reloading data.
[self.collectionView performBatchUpdates:^{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
[self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
[self.addNewDocumentArray removeObjectAtIndex:sender.tag];
} completion:^(BOOL finished) {
[self.collectionView reloadData];
}];
This help me to remove all the crash and assertion failures.