the code below works
#interface MyUICollectionViewCell : UICollectionViewCell {
}
#implementation UICollectionViewCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self = [super initWithFrame:frame];
if (self) {
_moreView = [[UIView alloc] init];
_moreView.backgroundColor = [UIColor clearColor];
_moreView.frame = CGRectMake(self.frame.size.width - 70, 0, 45, 37);
[self addSubview:_moreView];
_moreView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapMoreView = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(goToDoSomething)];
[_moreView addGestureRecognizer:tapMoreView];
}
return self;
}
return self;
}
- (void)doSomeThing
{
}
when I touch the subview (gray block), it will trigger the event,
but I change to XIB(the sub view links to IBOutlet mUIView), and change the code as below, goToDoSomething cannot be triggered.
Your comment welcome
#interface MyUICollectionViewCell : UICollectionViewCell {
IBOutlet UIView *mUIView;
}
#implementation UICollectionViewCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self = [super initWithFrame:frame];
if (self) {
NSArray *nibView = [[NSBundle mainBundle] loadNibNamed:#"MyUICollectionViewCell"owner:self options:nil];
UIView *bw = [nibView objectAtIndex:0] ;
bw.userInteractionEnabled = YES;
[self.contentView addSubview:bw];
mUIView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapMoreView = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(goToDoSomething)];
[mUIView addGestureRecognizer:tapMoreView];
}
return self;
}
return self;
}
- (void)doSomeThing
{
}
Related
I have made a UITableViewCell with xib, inside it I have made a UIView and made IBoutlet in my custom tableViewCell class. I want to set the border color of that UIView.
My code in tableViewCell.h:
#property (weak, nonatomic) IBOutlet UIView *circleView;
In tableViewCell.m:
#import "OUSTProfileTableViewCell.h"
#implementation OUSTProfileTableViewCell
//#synthesize circleView = _circleView;
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
self.circleView.layer.cornerRadius = 3; // this value vary as per your desire
self.circleView.layer.masksToBounds = YES;
self.circleView.layer.borderWidth = 2.0;
self.circleView.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor lightGrayColor]);
}
return self;
}
#end
But it's not working.
Put code inside
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
self.circleView.layer.cornerRadius = 3; // this value vary as per your desire
self.circleView.layer.masksToBounds = YES;
self.circleView.layer.borderWidth = 2.0;
self.circleView.layer.borderColor = [UIColor lightGrayColor].CGColor;
}
I decided to opt out of using a Xib to generate my custom UICollectionViewCell (StoneCell), so I've been struggling with how to go about properly initializing it programmatically.
I implemented:
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize size = [MainScreen screen];
CGFloat width = size.width;
CGFloat item = (width*60)/320;
return CGSizeMake(item, item);
}
as well as:
[self.collectionView registerClass:[StoneCell class] forCellWithReuseIdentifier:#"stoneCell"];
in my UICollectionView controller.
In my StoneCell.m, I tried the following:
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
StoneCell* stone = [[StoneCell alloc] initWithFrame:frame];
self = stone;
}
return self;
}
but to no avail. When I build and run, I crash on self = [super initWithFrame:frame]; When I check the value of frame, it's correctly set at {{0,0},{70,70}} which is what it should be on the 6s. However, the object stone (as well as self) are both reported as nil.
Clearly this is not correct, so I wanted to know how to initialize the cell properly.
I am also properly dequeuing the cell in:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
so that's taken care of.
Your initializer should be
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
return self;
}
With your original implementation
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
StoneCell* stone = [[StoneCell alloc] initWithFrame:frame];
self = stone;
}
return self;
}
you have an infinite recursion of initWithFrame.
// In AMAImageViewCell.h
#import <UIKit/UIKit.h>
#interface AMAImageViewCell : UICollectionViewCell
#property (strong, readonly, nonatomic) UIImageView *imageView;
#end
// In AMAImageViewCell.m
#implementation AMAImageViewCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_imageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
_imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_imageView.clipsToBounds = YES;
_imageView.contentMode = UIViewContentModeScaleAspectFill;
_imageView.layer.cornerRadius = 0.0;
[self.contentView addSubview:_imageView];
}
return self;
}
#end
/******In the class where you have to use **********/
[self.collectionView registerClass:[AMAImageViewCell class] forCellWithReuseIdentifier:ImageCellIdentifier];
// In cellForItemAtIndexPath
AMAImageViewCell *cell = [collectionViewLocal dequeueReusableCellWithReuseIdentifier:ImageCellIdentifier
forIndexPath:indexPath];
I'm using a custom UIView in myViewController. I have a SampleView which is my custom view. I have my class as..
SampleView.h
#property (strong, nonatomic) IBOutlet UIView *mainView;
#property (strong, nonatomic) IBOutlet UIButton *sampleButton;
and in the SampleView.m
I have added a basic init
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self commonInit];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
- (void)commonInit
{
UIView *view = nil;
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:#"SampleView"
owner:self
options:nil];
for (id object in objects) {
if ([object isKindOfClass:[UIView class]]) {
view = object;
break;
}
}
if (view != nil) {
_mainView = view;
view.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:view];
[self setNeedsUpdateConstraints];
}
}
Now in my ViewController I want to change the property of the title on the sampleButton...
SampleView *sampleView = [[SampleView alloc]init];
sampleView.sampleButton.titleLabel.text = #"Hello";
[self.view addSubview:sampleView];
There is no change in the SubView's button text. Please do help me about the same.
Thanks.
I think
- (void)commonInit
{
//UIView *view = nil;
UIView *view=[[UIView all]init];
........
}
I have created a custom tableviewcell class and the cells load perfectly, however, when I click on them they disappear and come back when I click on a different cell. Can anyone help me understand why? Thanks in advance!
#import "CustomTableViewCell.h"
#implementation CustomTableViewCell
UIView *backgroundView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
UIView *visibleBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(29, 0, backgroundView.bounds.size.width -58, backgroundView.bounds.size.height)];
[visibleBackgroundView setBackgroundColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"tableViewCell.png"]]];
[backgroundView addSubview:visibleBackgroundView];
self.backgroundView = backgroundView;
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
if (selected) {
UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
UIView *visibleSelectedBackground = [[UIView alloc] initWithFrame:CGRectMake(29, 0, backgroundView.bounds.size.width -58, backgroundView.bounds.size.height)];
[visibleSelectedBackground setBackgroundColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"selectedTableViewCell#2x.png"]]];
[selectedBackgroundView addSubview:visibleSelectedBackground];
self.selectedBackgroundView = selectedBackgroundView;
}
}
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
[self setHighlighted:NO];
}
}
I was able to fix it with the following solution
#import "CustomTableViewCell.h"
#implementation CustomTableViewCell{
UIView *backgroundView;
UIView *visibleBackgroundView;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
visibleBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(29, 0, backgroundView.bounds.size.width -58, backgroundView.bounds.size.height)];
[visibleBackgroundView setBackgroundColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"tableViewCell.png"]]];
[backgroundView addSubview:visibleBackgroundView];
self.backgroundView = backgroundView;
[self setSelectionStyle:UITableViewCellSelectionStyleNone];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
if (selected) {
}
}
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
[super setHighlighted:highlighted animated:animated];
if (highlighted) {
[visibleBackgroundView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"selectedTableViewCell.png"]]];
} else{
[visibleBackgroundView setBackgroundColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"tableViewCell.png"]]];
}
}
I am using this code for multiple cell types in a UITableView
The problem is that the cell text is invisible. The code for cellForRowAtIndexPath as well as the cell class code is given below:
code:
static NSString *kCellIdentifier = #"NewsViewControllerTableCell";
static NSString *kCellIdentifier2 = #"SubscribeCell";
if ((indexPath.row==0) && ([[NSUserDefaults standardUserDefaults] boolForKey:#"subscribeButtonOption"]))
{
SubscribeCell* cell = (SubscribeCell*)[tableView dequeueReusableCellWithIdentifier:kCellIdentifier2];
if (cell == nil) {
cell = [[[SubscribeCell alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 35.0) reuseIdentifier:kCellIdentifier2] autorelease];
cell.contentView.backgroundColor = kColorR53G53B53;
cell.subscribeLabel.font = kLucidaSansStdFontBold_14;
cell.subscribeLabel.textColor = [UIColor whiteColor];
}
cell.subscribeLabel.textColor=[UIColor redColor];
cell.subscribeLabel.text = #"+ SUBSCRIBE TO NEWSLETTER";
cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
cell.selectedBackgroundView.backgroundColor =kColorR53G53B53;
[cell setNeedsDisplay];
return cell;
}
else
{
//another cell
}
=========
header:
#import <UIKit/UIKit.h>
#interface SubscribeCell : UITableViewCell{
UILabel *subscribeLabel;
}
#property(nonatomic, retain) UILabel *subscribeLabel;
#end
and implementation class:
#import "SubscribeCell.h"
#implementation SubscribeCell
#synthesize subscribeLabel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
subscribeLabel=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 323.0, 40.0)];
subscribeLabel.textColor=[UIColor whiteColor];
self.backgroundColor=kColorR53G53B53;
}
return self;
}
Check to see if subscribeLabel is nil. You're creating it in initWithNibName:bundle: but are initializing with initWithFrame:reuseIdentifier:, so it's not reaching your label creation code.
If I try to compile your code, I get an error message stating that UITableViewCell does not declare a method called 'initWithNibName: bundle:'. You should use the proper initialization method 'initWithStyle: reuseIdentifier:'. You also forget to add the subscribeLabel to the contentView of the cell.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
subscribeLabel=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 323.0, 40.0)];
subscribeLabel.textColor=[UIColor whiteColor];
[self.contentView addSubview:subscribeLabel];
self.backgroundColor=kColorR53G53B53;
}
return self;
}