cellForItemAtIndexPath not called - objective-c

I'm trying to push from a view controller to a collection view controller.
UICollectionViewLayout *collectionViewLayout = [[UICollectionViewLayout alloc] init];
xraylab *xraylabvc = [[xraylab alloc]initWithCollectionViewLayout:collectionViewLayout];
[self.navigationController pushViewController:xraylabvc animated:YES];
The collection view controller will be displayed with a blank black screen.
numberOfSectionsInCollectionView returns 1
numberOfItemsInSection returns a non-zero integer
My problem is that cellForItemAtIndexPath is not called.
Delegate and datasource of the collection view are set.
Any suggestions?

numberOfItemsInSection should return rowcount > 0 for cellForItemAtIndexPath to execute
Check Delegate and datasource of the collection view are set

I had the same problem and it was driving me crazy here is the code that worked.
UICollectionViewFlowLayout *CollLayout = [[UICollectionViewFlowLayout alloc] init];
UICollectionView *AttachmentsView = [[UICollectionView alloc] initWithFrame:CGRectMake(20, CurrentY, 640, 300) collectionViewLayout:CollLayout];
//AttachmentsView.backgroundColor = [UIColor whiteColor];
[AttachmentsView registerClass:[Cell class] forCellWithReuseIdentifier:#"ReuseCell"];
[VaribleStore SharedInstance].Attachments = [[NSMutableArray alloc] initWithObjects:[UIColor yellowColor],[UIColor redColor],[UIColor blueColor], nil];
AttachmentsView.delegate = self;
AttachmentsView.dataSource = self;
[self.ScrollView addSubview:AttachmentsView];
[AttachmentsView reloadData];
The key was to use the UICollectionViewFlowLayout, the reason is that UICollectionViewLayout is an abstract class, and can not be used for this purpose. after i made that switch ios called cellForItemAtIndexPath as normal.

You can't use UICollectionViewLayout as is. It's an abstract base class and it has to be subclassed. You can use UICollectionViewFlowLayout for grid-like views if you don't want to write your own.

Related

How to create a UIAlertView with table view

I need to add a tableview inside a UIAlertView in Objective-C.
I tried to add a tableview as a subview of the alert view, but this is not working.
-(IBAction)ShowAlertWithTable
{
UIAlertView *select_dialog ;
select_dialog = [[[UIAlertView alloc] init] retain];
[select_dialog setDelegate:self];
[select_dialog setTitle:#"Alphabets"];
[select_dialog setMessage:#"\n\n\n\n"];
[select_dialog addButtonWithTitle:#"Cancel"];
UITableView * Type_table = [[UITableView alloc]initWithFrame:CGRectMake(20, 45, 245, 90)];
Type_table.backgroundColor = [UIColor redColor];
idType_table.delegate = self;
idType_table.dataSource = self;
[select_dialog addSubview:Type_table];
[Type_table reloadData];
[select_dialog show];
}
Why don't you use this library? It has all what you needs!
https://github.com/mindbrix/SBTableAlert
The above developer is using this 'Library' for AlertView
https://github.com/mindbrix/TSAlertView
The default alert isn't meant to take subviews. Although, you can write your own custom UIView that can meet your needs.
I know it is late to answer the question. I want the same functionality in my application and after a little search I found my answer.
You can use UIAlertView's setValue:<your UITableView> forKey:#"accessoryView" property to add your UITableView in UIAlertView. You can create the UITableview programmatically to set value above. You can create and set any object like UIView, UIImageView etc.
For more detail Please refer this answer

Using a UICollectionView over an SKScene

I have an app with several SKScenes. To keep it snappy, I have a single UIViewController, that handles 1 SKView.
I'm trying to add a UICollectionView to one of the SKScenes. However the problem comes when I try to set the delegate for the collection view to the SKScene initialising it.
I initialise it here:
- (id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size]) {
//Initialise collectionView
UICollectionViewLayout *layout = [[UICollectionViewLayout alloc] init];
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height) collectionViewLayout:layout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
[_collectionView setBackgroundColor:[UIColor redColor]];
}
return self;
}
After initialising, I add the collection view as a subview of the UIViewController's view by calling:
- (void)didMoveToView:(SKView *)view
{
[self.view addSubview:_collectionView];
}
But the delegate or datasource methods aren't called. I've set up the header:
#interface BrowseScene : SKScene <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
I'm guessing that despite setting self as the delegate, once the collection view is added, it is added as a subview of the UIViewController's SKView, and therefore has no reference to the SKScene that initialised it. I tried calling:
_collectionView.delegate = self.view.scene;
But I get a compiler error saying SKScene is an incompatible type for UICollectionViewDelegate.
So my question is, how best to approach this?
It turns out, the problem lay in how I was initialising the UICollectionView, not where. And it's perfectly ok to set an SKScene as the delegate class.
The main reason the delegates weren't being called was because I'd initialised the collectionView with an empty layout object (as UICollectionViewLayout is just an abstract class). I initialised with a standard UICollectionViewFlowLayout instead, and the methods were then called when the class initialised.
I was also missing a call to
[_collectionView registerClass:[collectionViewCell class] forCellWithReuseIdentifier:#"collectionViewCell"];
(In the complete code, I had calls to reloadData, so that wasn't the problem here).

How do I change the background color of a view controller managed by a UIPageViewController?

I created an onscreen tutorial for my iOS app.
To accomplish this I'm using a UIPageViewController which is managing 3 viewControllers.
- (void)setupContentViews{
UIViewController *screenTutorial1 = [[UIViewController alloc] initWithNibName:#"ScreenTutorial_1ViewController" bundle:nil];
UIViewController *screenTutorial2 = [[UIViewController alloc] initWithNibName:#"ScreenTutorial_2ViewController" bundle:nil];
tutorialPages = #[screenTutorial1, screenTutorial2];
}
Everything works great, except that when I got to change the background for screenTutorial1 or screenTutorial2 it never gets called. What's the reason for this? Is there a solution?
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
NSLog(#"line 30)");
}
After some experimentation it appears that if I add the code in UIPageViewController (see below) it sets the property. But what if I need to add any custom methods to my View Controllers? Do I need to do everything from UIPageViewController?
The problem is that at that point the view is nil so you can't change the backgroundColor.
You should subclass UIViewController and set the backgroundColor in viewDidLoad. And after that you should initialize the view controllers for the tutorialPages ivar like this:
YourUIViewControllerSubclass *screenTutorial1 = [[YourUIViewControllerSubclass alloc] initWithNimbName:#"ScreenTutorial_1ViewController" bunble:nil];
Update
Update your method setupContentViews like this:
- (void)setupContentViews
{
ScreenTutorial_1ViewController *screenTutorial1 = [[ScreenTutorial_1ViewController alloc] initWithNibName:#"ScreenTutorial_1ViewController" bundle:nil];
UIViewController *screenTutorial2 = [[UIViewController alloc] initWithNibName:#"ScreenTutorial_2ViewController" bundle:nil];
tutorialPages = #[screenTutorial1, screenTutorial2];
}

Create UIPopoverController from existing controller

I want to use a UIPopoverController in my application and was trying this example. The problem is that the view and the controller in that example are created from code.
UIViewController* popoverContent = [[UIViewController alloc]
init];
UIView* popoverView = [[UIView alloc]
initWithFrame:CGRectMake(0, 0, 200, 300)];
popoverView.backgroundColor = [UIColor greenColor];
popoverContent.view = popoverView;
I want to use an existing controller with it's xib file for the popup. How do I link the popup to an existing controller? And do I need to create the controller's view in some special way for it to match the dimension of the popup?
Yes you can:
MyUIViewController* content = [[MyUIViewController alloc] initWithNibName:#"myNib" bundle: nil];
// additional initialization in loadView
UIPopoverController* aPopover = [[UIPopoverController alloc]
initWithContentViewController:content];
If you want to use an existing xib, just initialize your viewController using the nitWithNibName:bundle: method. When you init using the xib your viewController's view hierarchy will be instantiated for you.
UIViewController* popoverContent = [[UIViewController alloc] initWithNibName:#"yourXibName" bundle:nil];
Don't worry about sizing the view when you initialize - the view gets resized in the example code you cite anyway on the next line when the property contentSizeForViewInPopover is set.

objective-c init UITableViewController with data array?

I have four buttons, which have to push the same uiTableViewController, with different data in every case. I think I need to implement an initializer for my table and pass a NSArray. and then I'll check which button is pushed with the sender property.
Is that ok? How can I implement this initializer? I searched in the doc but I haven't get solution yet.
in your view controller you can initialize tableview by [UITableView alloc] initWithFrame:self.view.bounds]; and put this initialization in viewdidload method of your view controller,you don't need xib now.
to be specific use
[self.tableView = [UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.000];
self.tableView.bounces=YES;
[self.view addSubview:self.tableView];
self.tableView.delegate = self;
self.tableView.dataSource = self;