I have a table and custom cell from NIB. Basically what I need is to slide the custom cell from the table to the right side. It has to be smooth, and the table itself should not collapse. (The cell will get back after the action is done). I have some number of sections, and 1 row/section, so basically I need to move with the section. Thanks a lot for the answer!
My cell.m file looks like this:
#import "MIKETableViewCell.h"
static NSString *CellTableIdentifier = #"MIKETableViewCell";
#implementation MIKETableViewCell
#synthesize timeLabel = _timeLabel;
#synthesize priceLabel = _priceLabel;
#synthesize infoLabel = _infoLabel;
- (void)awakeFromNib
{
// Initialization code
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:CellTableIdentifier];
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
add this:
-(void)layoutSubviews
{
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tableCellClicked)];
tapGesture.numberOfTapsRequired = 1;
[self addGestureRecognizer:tapGesture];
}
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've got a tableview controller with custom cells. For each type of cell, I created a prototype cell in the storyboard as well as a class.
Here's one of the cells:
The cell has a circular button that contains a number.
I'm trying to modify the value of the number in my cellForRowAtIndexPath method like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
TrackMilstoneCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"TrackMilstoneCell"];
if (cell == nil) {
cell = [[TrackMilstoneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"TrackMilstoneCell"];
}
cell.backgroundColor = cell.contentView.backgroundColor;
cell.milestoneNumber.backgroundColor = UIColorFromRGB(0xA875E1);
[cell.milestoneNumber.titleLabel setText:#"2"];
return cell;
} ...
However, I'm getting a very unpredictable behavior. Every time the tableview is reloaded I sometimes get 1 (the default in storyboard), and sometimes 2 (which is what i want).
This is the code for my (TrackMilstoneCell) class:
#import "TrackMilstoneCell.h"
#implementation TrackMilstoneCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
-(void)layoutSubviews
{
[self viewSetup];
}
-(void)viewSetup
{
self.milestoneNumber.layer.masksToBounds = NO;
self.milestoneNumber.layer.borderColor = [UIColor whiteColor].CGColor;
self.milestoneNumber.layer.borderWidth = 4;
self.milestoneNumber.layer.cornerRadius = self.milestoneNumber.bounds.size.width / 2.0;
}
- (void)awakeFromNib
{
// Initialization code
[super awakeFromNib];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
The problem is with reusability, so here the best solution is reset the label in prepareForReuse method like this:
- (void)prepareForReuse {
[super prepareForReuse];
[self.milestoneNumber setTitle:#"" forState:UIControlStateNormal];
}
And while configuring the cell, set title like:
[self.milestoneNumber setTitle:#"2" forState:UIControlStateNormal];
I think you should set default stage of your button in awakeFromNib.
In your custom table view cell class:
- (void)awakeFromNib
{
// Initialization code
[super awakeFromNib];
self.milestoneNumber.titleLabel.text = #"";
}
kaushal's suggestion did the trick!
Instead of setting the title like this:
[cell.milestoneNumber.titleLabel setText:#"2"]
I did:
[cell.milestoneNumber setTitle:#"2" forState:UIControlStateNormal];
And now it's working just fine. Though I'm not sure why exactly.
I have a custom cell that I have made in the iterfacebuilder and connected to my .h file.
And then in my CustomCell.m file I have
#implementation TwoMealsTableViewCell
#synthesize dayLabel, firstMealBtn, secondMealBtn, indexPath;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
dayLabel.textColor = [UIColor orangeColor];
if (self) {
dayLabel.textColor = [UIColor orangeColor];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
And I have the cell all set up in the tableview, and I see the buttons, and labels, but I don't see the color change?
Why is this.
Thanks for the help in advance!!!
This will change the cell color
dayLabel.textColor = [UIColor orangeColor];
I'm trying to add a gesture to a subview of a UICollectionViewCell make with xib, I'm doing this:
.h
#interface MyCell : UICollectionViewCell <UIGestureRecognizerDelegate>
#property (weak, nonatomic) IBOutlet UIView *containerButton;
#end
.m
#implementation MyCell
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self initialize];
}
return self;
}
- (void)initialize
{
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePanGesture:)];
[panGestureRecognizer setDelegate:self];
if (self.containerButton) {
NSLog(#"ok"); //not enter here
[self.containerButton addGestureRecognizer:panGestureRecognizer];
}
}
-(void)prepareForReuse {
[super prepareForReuse];
if (self.containerButton) {
NSLog(#"ok 2");
}
}
I have created the UICollectionViewCell subclass connected with the xib file, where I have created the container button view, if I try to add the gesture in the initialize method, the containerButton is nil, so doesn't work, but in the prepareForReuse method is not empty, I can add the gesture there? or I can do it in other place?
Try this:
- (void)awakeFromNib
{
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePanGesture:)];
[panGestureRecognizer setDelegate:self];
if (self.containerButton) {
NSLog(#"ok"); //not enter here
[self.containerButton addGestureRecognizer:panGestureRecognizer];
}
}
I'd like to add a UIButton to a custom UITableViewCell (programmatically). This is easy to do, but I'm finding that the "performance" of the button in the cell is slow - that is, when I touch the button, there is quite a bit of delay until the button visually goes into the highlighted state. The same type of button on a regular UIView is very responsive in comparison.
In order to isolate the problem, I've created two views - one is a simple UIView, the other is a UITableView with only one UITableViewCell. I've added buttons to both views (the UIView and the UITableViewCell), and the performance difference is quite striking.
I've searched the web and read the Apple docs but haven't really found the cause of the problem. My guess is that it somehow has to do with the responder chain, but I can't quite put my finger on it. I must be doing something wrong, and I'd appreciate any help. Thanks.
Demo code:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property UITableView* myTableView;
#property UIView* myView;
ViewController.m
#import "ViewController.h"
#import "CustomCell.h"
#implementation ViewController
#synthesize myTableView, myView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self initMyView];
[self initMyTableView];
}
return self;
}
- (void) initMyView {
UIView* newView = [[UIView alloc] initWithFrame:CGRectMake(0,0,[[UIScreen mainScreen] bounds].size.width,100)];
self.myView = newView;
// button on regularView
UIButton* myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton addTarget:self action:#selector(pressedMyButton) forControlEvents:UIControlEventTouchUpInside];
[myButton setTitle:#"I'm fast" forState:UIControlStateNormal];
[myButton setFrame:CGRectMake(20.0, 10.0, 160.0, 30.0)];
[[self myView] addSubview:myButton];
}
- (void) initMyTableView {
UITableView *newTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,100,[[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height-100) style:UITableViewStyleGrouped];
self.myTableView = newTableView;
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
}
-(void) pressedMyButton {
NSLog(#"pressedMyButton");
}
- (void)viewDidLoad {
[super viewDidLoad];
[[self view] addSubview:self.myView];
[[self view] addSubview:self.myTableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *customCell = [tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
if (customCell == nil) {
customCell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"CustomCell"];
}
return customCell;
}
#end
CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell
#property (retain, nonatomic) UIButton* cellButton;
#end
CustomCell.m
#import "CustomCell.h"
#implementation CustomCell
#synthesize cellButton;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// button within cell
cellButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[cellButton addTarget:self action:#selector(pressedCellButton) forControlEvents:UIControlEventTouchUpInside];
[cellButton setTitle:#"I'm sluggish" forState:UIControlStateNormal];
[cellButton setFrame:CGRectMake(20.0, 10.0, 160.0, 30.0)];
[self addSubview:cellButton];
}
return self;
}
- (void) pressedCellButton {
NSLog(#"pressedCellButton");
}
#end
On the table view, under section "scroll view" there is the option "delays content touches"... remove it and the delay on button is gone but in this way table scroll don't start dragging the button.
I don't think it has anything to do with what you're doing (I tested it, and it is a little slow, but I wouldn't call it "sluggish"). It probably has to do with the various gesture recognizers attached to a table view -- the operating system has to figure out what gesture is happening, and that may cause a slight delay. This is the log of tableView.gestureRecognizers:
2012-10-09 20:34:12.355 SlowButtonsInTableView[3635:c07] (
"<UIScrollViewDelayedTouchesBeganGestureRecognizer: 0x71b42b0; state = Possible; delaysTouchesBegan = YES; view = <UITableView 0x789f800>; target= <(action=delayed:, target=<UITableView 0x789f800>)>>",
"<UIScrollViewPanGestureRecognizer: 0x71b4940; state = Possible; delaysTouchesEnded = NO; view = <UITableView 0x789f800>; target= <(action=handlePan:, target=<UITableView 0x789f800>)>>",
"<UISwipeGestureRecognizer: 0x71b4e00; state = Possible; view = <UITableView 0x789f800>; target= <(action=handleSwipe:, target=<UITableView 0x789f800>)>; direction = right,left>",
"<UIGobblerGestureRecognizer: 0x71b5100; state = Possible; enabled = NO; view = <UITableView 0x789f800>>"
)
With scrolling on table view not enabled the delay disappear completely, probably the delay is caused by the gesture necessary for scrolling