Instead of UICollectionView a black screen is displayed - ios

I'm trying to reimplement the infinitive scrolling UICollectionView seen here. Things that were missing for me:
ViewController.h:
#interface ViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate>
#end
DataCell.h:
#interface DataCell : UICollectionViewCell
#property (nonatomic, strong) UILabel *label;
#end
DataCell.m:
#import "DataCell.h"
#implementation DataCell
-(instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if(self){
self.label = [[UILabel alloc] initWithFrame:self.bounds];
self.autoresizesSubviews = YES;
self.label.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
self.label.textAlignment = NSTextAlignmentCenter;
self.label.adjustsFontSizeToFitWidth = YES;
[self addSubview:self.label];
}
return self;
}
#end
CustomCollectionView.h:
#interface CustomCollectionView : UICollectionView
#end
For the whole project I used a storyboard and a normal UIViewController. On this view controller I added a UICollectionView in Interface Builder. I connected the outlet from the collection view with my view controller and set up the datasource and delegate methods again to my view controller. I also set the custom class of the UICollectionViewCell and the reuse identifier in Interface Builder.
So everything should work but I only get a black screen. What I'm missing? You can download the whole project here.

You are configuring correctly the CollectionView, just that you forgot the color of the label :)
[self.label setTextColor:[UIColor whiteColor]];
Hope it helps!

You need to manually set the background color of the collection view in the storyboard.
By default it is black (although not showing that in the storyboard editor)

I had the same issue. The black screen seems to be an indicator of no data available with collection view to display. Try changing the background color of the collection view, if that changed color got to display, your collection view is working. And then add some imageview to the collection view with tag (Ex. give a value 100 with the tag value for the image view) and with the cellforItemAtIndexPath set the images to the image view.
(You can do this with custom cell. But for now, to get the collection view work, the assignment with tag for the imageview suits better)
UIImageView * ImageView = (UIImageView *)[cell viewWithTag:100];
ImageView.image = [UIImage imageNamed:[images objectAtIndex:indexPath.row]];

[self.collectionView registerNib:[UINib nibWithNibName:#"ProductCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:#"ProductCollectionViewCell"];
self.collectionView.backgroundColor = [UIColor clearColor];

it happened to me that both the collectionView and the collection view cell had transparent backgrounds

In Swift,
self.label.textColor = UIColor.whiteColor()

Related

UIButton not working in UITableview

Have UITableviewcell --> inside which i have 4 different UIView and 3 of the views has UIButtons. I want to make UIButton clickable. For the first time the buttons are clickable but when i go to next screen and come back, the buttons don't work. Please let me know how to implement this? Thanks!
PHMyTripView *tripsView=[[PHMyTripView alloc] initWithFrame:CGRectMake(10, 5, self.frame.size.width-20, cell.frame.size.height)];
tripsView.onBaggageClick = ^{
[weakSelf handleBaggagePurchaseTapped];
};
if ([data count]>0) {
[tripsView fillTripData:[data firstObject]];
[tripsView showAncillaries:self.predictiveManager.upcomingCDAirBookingInfo];
}
[cell bringSubviewToFront:tripsView.bagsButton];
[cell.viewPlaceHolder addSubview:tripsView];
This happens because the cells are reusable and if you go to another screen the button may kinda mess round into different cell in UI or functionally.
The way to solve this is to add tag. You can define cell.tag = indexPath.row * 10 or so.
Then when draw the button, you check the condition, if cell.tag == ?, then add your button.
Thank you everyone for your response but they din't work in my case.
Here is the answer. Since the UITableview was reloading when it comes back to the screen.The frames were mis placed and Hence the button was not able to click.
Used this one line code which worked fine.
self.tripsView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
You should turn off delaysContentTouches on each UITableView subview, which is an instance of UIScrollView class.
Subclass UITableView and override initWithFrame method:
.h file
#interface NoDelaysOnTouchTableView : UITableView
#end
And .m file
#import "NoDelaysOnTouchTableView.h"
#implementation NoDelaysOnTouchTableView
- (id) initWithFrame: (CGRect) frame
{
self = [super initWithFrame: frame];
if (self) {
for (id view in self.subviews) {
if ([NSStringFromClass([view class]) isEqualToString: #"UITableViewWrapperView"]) {
if ([view isKindOfClass: [UIScrollView class]]) {
// turn OFF delaysContentTouches in the hidden subview
UIScrollView* scroll = (UIScrollView*)view;
scroll.delaysContentTouches = NO;
}
break;
}
}
}
return self;
}
#end
Next - use this subclass (create an instance of NoDelaysOnTouchTableView) to be able to press UIButton immediately.
I think that the causes would be multiples.
You can try:
check the constraints for each button in your cell
in cellForRow you can do addTarget: for each button with relative #selector and in this method check the object (datasource) associated to button.
do not use addSubView in your cell, instead use a Xib (or define your cell in a storyboard within tableView) so you can set the constraints.
I Hope, i've helped you.
Not specifically about this issue, but for some people it may be the case.Try to add subviews in contentView of the cell.
Instead of addSubview(textField) use contentView.addSubview(textField)

Change the selected cell background colour using UIAppearance

I need to change the selected cell background colour for all the cells in my app. As I know there is a way to use UIAppearance protocol for this purposes. Is it possible to realize this by the category for UITableViewCell?
Using appearance proxy you can colour all cells. Don't know if you can target specific category.
To do the colouring put following code in your AppDelegate.m file:
Put [self customCellBackground];
in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions
and somewhere at the end:
- (void)customCellBackground {
UIView *cellBackgroundView =[[UIView alloc] init];
cellBackgroundView.backgroundColor = [UIColor blackColor];
[[UITableViewCell appearance] setSelectedBackgroundView:cellBackgroundView];}
As null's answer is not for selected cell backgrounds and Armands L.'s answer did not work consistently for me (selecting cells by 'user-tap' did work, but programmatical cell selection showed strange results (like sometimes the selected background was not visible, or did not fill the cell's height properly...).
I found a custom solution that worked:
Subclass UITableViewCell
Initialize self.selectedBackgroundView in init and
Add custom UIColor property with UI_APPEARANCE_SELECTOR for custom selected background color
.h file:
#property (nonatomic) UIColor* selectedCellBackgroundColor UI_APPEARANCE_SELECTOR;
.m file:
in init method(s):
self.selectedBackgroundView = [[UIView alloc] init];
and last but not least the setter function for the color:
- (void) setSelectedCellBackgroundColor:(UIColor*) color {
_selectedCellBackgroundColor = color;
self.selectedBackgroundView.backgroundColor = color;
}
You can't do this direct to UITableViewCell, but you can do it for its contentView:
[[UIView appearanceWhenContainedIn:[UITableViewCell class], nil] setBackgroundColor:[UIColor redColor]];
Note that it will change all the subViews bg color.
Another option is writing a category or subclass the UITableViewCell with UI_APPEARANCE_SELECTOR mark, check this question:
iOS: Using UIAppearance to define custom UITableViewCell color

Changing Background of Edit Button - UITableView iOS7

I have a UITableView which when edited looks as follows :
Is it possible to somehow change the background behind the delete symbol so that it is not white ?
I hope this help you
UIView *cellBackView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
cellBackView.backgroundColor = [UIColor clearColor];
cell.cellBackgroundView = backView;
The problem ist that contentView gets shifted to the right when you are in editing mode, and because of this all its subviews will move to the right as well.
If your background is an imageView you should not add is as subview to contentView, set it as backgroundView of the cell instead.
Since you can't setup backgroundView from interface builder I would recommend to create a custom subclass of your cell and put the background creation into awakeFromNib.
- (void)awakeFromNib {
[super awakeFromNib];
self.backgroundView = [[UIImageView alloc] initWithImage:...];
}

why my actual UI layout is not same with my designed layout in xib

this is what I designed in xib, and it's my expectation also.
My ViewControlle.h file:
#interface DetailHeadViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIScrollView *slideScrollView;
#property (weak, nonatomic) IBOutlet UIImageView *smallImageView;
#property (weak, nonatomic) IBOutlet UIImageView *barBGVImageView;
#end
My ViewController.m file (part of the source code)
- (void)viewDidLoad
{
[super viewDidLoad];
slideScrollView.pagingEnabled = YES;
slideScrollView.showsHorizontalScrollIndicator = NO;
slideScrollView.showsVerticalScrollIndicator = NO;
UIImageView * page1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"headSection_hot1"]];
[slideScrollView addSubview:page1]; //Update 3
}
the result is this,
But actually the correct result is this,
Anyone can help to point out what the problem is?
Thanks in advance
update (remove bar image)
for easier, I remove bar image in xib and also comment out the relatived code.
Please see the following picture.
update 2
IB
To me it looks like your scroll view frame is not set correctly. It should probably be set to 0,0,480,320.
It also looks like the transparency isn't set on the BarImageView. Select the BarImageView in the xib, open up the inspector window, click on the 4th tab, set the background color to Clear Color and reduce the alpha.
You may also want to post how your adding the image to the scroll view as that could create issues as well.
Update 1:
In the viewDidLoad of your DetailHeadViewController try this:
self.scrollView.frame = self.view.bounds;
UIImageView * page1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"headSection_hot1"]];
page1.frame = self.view.bounds;
[self.slideScrollView addSubview:page1];
self.scrollView.contentSize = self.view.bounds.size; // Note that the contentSize must be changed
// from this if you want to scroll.

Change background on a UITableView embedded in a UIViewController on iOS 5

I am trying to change the grey gradient background on an embedded UITableView to the color I have set on the parent view in the Storyboard.
I have been looking around, and found the following threads:
Change iPhone tableview (style grouped) background color while preserving texture
How can I set the background of UITableView (the tableview style is “Grouped”) to use an image?
UITableView backgroundColor always gray on iPad
I have an IBOutlet in the parent controller connecting the two views.
My implementation looks as follows:
- (void)viewDidLoad {
[super viewDidLoad];
[activeShipmentsTable setBackgroundView:nil];
[activeShipmentsTable setBackgroundView:[[[UIView alloc] init] autorelease]];
[activeShipmentsTable setBackgroundColor:UIColor.clearColor];
}
What am I doing wrong?
Thanks.
Try setting et the background color on the table's background view, not the table itself.
UIView *view = [[[UIView alloc] init] autorelease];
view.backgroundColor = [UIColor redColor];
self.tableView.backgroundView = view;

Resources