Detecting if a UITableViewCell is a reused cell - ios

In my app I have UITableView cells with multiple buttons. 4 UIButtons per cell. For each UIButton I want to add a UILongPressGestureRecognizer. Code below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
FriendViewCell *cell = (FriendViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
for(int i =0; i<4; i++) {
UIButton *button = cell.buttons[i];
UILabel *label = cell.labels[i];
[button setTag:(int)indexPath.row*4+i];
[button addTarget:self action:#selector(friendTapped:) forControlEvents:UIControlEventTouchUpInside];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(longPressFriend:)];
[button addGestureRecognizer:longPress];
}
}
I just realised though that if a cell is reused then I am adding a gesture multiple times per button. Is there a way to detect if the cell created is reused or new? I don't think I can move the code to the FriendViewCell class because my gesture target friendTapped: is in my UITableViewController. Any pointers will be greatly appreciated! thanks

A cleaner way would be to create a custom class for your Button. This class would have an UILongPressGestureRecognizer created at initialization and a delegate (your controller) that will be called when the gesture is triggered.
.h
#class MyLongPressedButton
#protocol MyLongPressedButtonDelegate
- (void)buttonIsLongPressed:(MyLongPressedButton *)button;
#end
#interface MyLongPressedButton
#property (nonatomic, weak) id<MyLongPressedButtonDelegate > delegate
#end
.m
-(id)init {
if (self = [super init]) {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(longPress:)];
[self addGestureRecognizer:longPress];
}
return self;
}
-(void)longPress:(id)sender {
[_delegate buttonIsLongPressed:self]
}

First of all, there are fast ways to solve this - you can check the existing gesture recognizers on the button whether there is a long press gesture recognizer.
A slightly better solution is to define a property on the cell, e.g.
#property (nonatomic, assign) BOOL recognizerAdded
However, the best solution is to do this inside the cell class
#implementation FriendViewCell
- (void)awakeFromNib {
//add the recognizers
}
#end
Note that you your table delegate shouldn't care about the structure of your cell, it's the cell's responsibility to setup itself properly.
Of course, you will need a delegate on the cell to notify you about the action but it will be a cleaner solution.

I would recommend placing the code inside your cell and creating a delegate method that will return a reference to the cell.
E.g.
#protocol myAwesomebuttonCellDelegate <NSObject>
- (void)myAwesomeCell:(MyAwesomeCell *)cell buttonPressed:(UIButton *)btn;
#end
Then in your view controller you can use:
NSIndexPath *index = [tblView indexPathForCell:cell];
to get the row / section

Related

Custom table view cell with button

I am using storyboard. I have a table view with 1 Prototype cell, and have four rows in table view. Each cell has a label,image view and a button. Now I want to show different data on next View Controller say(DetailsVc), each time when button is pressed.
Row1- Button click will open DetailsVC with some data.
Row2- Button click will open DetailsVC with different data.
and same process for remaining rows.
Any suggestions.
#implementation DetailsVC
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
EmpEngmntTableViewCell *emp;
if((emp.viewGalleryBtn.tag = 1))
{
NSLog(#"tag num for 1st row is %ld",(long)emp.viewGalleryBtn.tag);
self.title = #"Team Building Session";
}
}
The best is to use a delegate pattern. I added an answer here, you can use it for example.
Here is an example enclosed:
Custom Cell .h Class:
#protocol DetailVCProtocol <NSObject>
-(void)loadDetailScreenWithIndex:(NSInteger)rowIndex;
#end
#property (nonatomic, retain) id<DetailVCProtocol> delegate;
#property (nonatomic, retain) IBOutlet UIButton *btn;
Then Synthesize it in .m file
Add this in m file:
-(IBAction)loadDetails:(id)sender
{
// ..... blah blah
[self.delegate loadDetailScreenWithIndex:btn.tag];
}
The viewcontroller that loads this cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// create cell here
cell.btn.tag = indexPath.row;
cell.delegate = self;
}
-(void)loadDetailScreenWithIndex:(NSInteger)rowIndex
{
// Cretae DetailVC with parameter `rowIndex` to populate data
[self presentViewController:detailVC animated:YES completion:nil];
}
In function - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
use this code
cell.btn.tag = indexPath.row;
[cell.btn addTarget:self action:#selector(btnTpd:) forControlEvents:UIControlEventTouchUpInside];
- (IBAction)btnTpd:(id)sender
{
UIButton *btn = (UIButton *)sender;
\\You can get which button is pressed by btn.tag
\\Here call to next ViewController and pass data
}
In cellForRowAtIndexPath
cell.optionsBtn.tag=indexPath.row;
[cell.optionsBtn addTarget:self action:#selector(showMyOptionBtn:) forControlEvents:UIControlEventTouchUpInside];
-(void)showMyOptionBtn:(UIButton *)sender
{
EventDetailViewController *EventDetail=[[EventDetailViewController alloc]init];
EventDetail.buttonTag=sender.tag;
[self.navigationController pushViewController:EventDetail animated:NO];
}
In EventDetailViewController class
if (self.buttonTag==1) {
// your condation
}
else
{
//your condation
}

Touch not responding with UITableView

I have a storyboard with a UINavigationViewController that holds a UITableView and a UIToolBar. The TableView populates well but doesn't respond when I touch a TableViewCell. It lits up but the selection doesn't persist and the handler function isn't called. When I make some sort of swipe over the cell it sometimes records the touch event and the handler is called. Looks like as if touchEvents aren't recognized or hold back. This is my setup:
The View component config of the NavigationView
The UITableView config
Remarkable is that if I touch the cell with 2 fingers it selects the cell...! So this handler gets called in that case:
- (void)viewDidLoad {
[super viewDidLoad];
self.selectedWerkzaamheden = self.model.selectedWerkorder.werkbonWerkzaamheden;
self.selectedWerkzaamheden = [NSMutableArray arrayWithArray:self.model.selectedWerkorder.werkbonWerkzaamheden];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
[self.myTableView reloadData];
}
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.selectedWerkzaamheidIndex = indexPath.row;
self.selectedWerkzaamheid = self.selectedWerkzaamheden[self.selectedWerkzaamheidIndex];
self.trashBtn.enabled = TRUE;
}
The declaration file:
#import "LmxViewController.h"
#import "LmxDropDownDataVO.h"
#interface LmxWerkzaamheidViewController : LmxViewController <UITableViewDataSource>
#property NSString *faultTitle;
#property NSString *faultMessage;
#property IBOutlet UIButton *selectWerkzaamheidBtn;
#property LmxDropDownDataVO *selectedWerkzaamheid;
#property NSInteger selectedWerkzaamheidIndex;
#property NSMutableArray *selectedWerkzaamheden;
#property IBOutlet UIBarButtonItem *submitBtn;
#property IBOutlet UITableView *werkzaamheidTableView;
#property (strong, nonatomic) IBOutlet UIBarButtonItem *trashBtn;
- (IBAction)selectWerkzaamheid:(id)sender;
- (IBAction)saveWerkzaamheidWithWerkorder:(id)sender;
- (IBAction)cancel:(id)sender;
#end
Might there be something wrong in the way I supply the cells?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableArray *werkzaamheden = self.selectedWerkzaamheden;
static NSString *simpleTableIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
NSInteger row = indexPath.row;
LmxDropDownDataVO *data = (LmxDropDownDataVO *)werkzaamheden[row];
cell.textLabel.text = data.naam;
return cell;
}
The problem was I did leave a piece of code in the viewDidLoad class that obscured the touch event of the table view cell.
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
Apparently this is blocking the touch events to reach the Table View Delegate function:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Thx for your contributions.

UIView vs UITableCellView to show user posts

My app has a home screen where I show user posts loaded from the server. My problem is that I use UIView for each post, but it takes up a lot of space (also the user keeps on scrolling making it more memory consuming). Ex:
UIView* box=[[UIView alloc]initWithFrame:CGRectMake(0, postY, maxWidth, 500)];
[box setTag:(NSInteger)[post_id[i] integerValue]];
[box setBackgroundColor:[UIColor whiteColor]];
//Profile pic+++
UIImageView* profile_img=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
profile_img.layer.cornerRadius=profile_img.frame.size.width/2; //Make it round
profile_img.layer.masksToBounds=YES; //Make it round
profile_img.layer.borderWidth=0.5;
profile_img.layer.borderColor=[rgb(214, 222, 231) CGColor];
[box addSubview:profile_img];
And so on...
Does anyone know a better way of doing this? I tough of UITableCellView but it seems to be odd doing this task this way
I recommend you use a UITableViewController and then subclass UITableViewCell to create a custom cell for you to display the data. A UITableViewController instance contains a UITableView and you display the data by setting it in your custom UITableViewCell subclass. So basically, a UITableView contains various UITableViewCell and use the delegate methods to respond to events and actions on your table.
// YourCustomTableViewCell.h
#import <UIKit/UIKit.h>
#interface YourCustomTableViewCell : UITableViewCell
// Setup the properties for the cell, e.g
#property (strong, nonatomic) IBOutlet UIImageView *profileImageView; // Connect to outlet in storyboard file
#end
// YourCustomTableViewCell.m
#import "YourCustomTableViewCell.h"
#implementation YourCustomTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.profileImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
}
return self;
}
- (void)awakeFromNib
{
// Use this method to setup frames and sizes of your properties
self.profileImageView.layer.cornerRadius = profileImageView.frame.size.width/2; //Make it round
self.profileImageView.layer.masksToBounds = YES; //Make it round
self.profileImageView.layer.borderWidth=0.5;
self.profileImageView.layer.borderColor = [rgb(214, 222, 231) CGColor];
}
#end
// YourTableViewController.h
#import <UIKit/UIKit.h>
#interface YourTableViewController : UITableViewController
#end
// YourTableViewController.m
#import "YourTableViewController.h"
#import "YourCustomTableViewCell.h"
#interface YourTableViewController ()
#end
#implementation YourTableViewController
- (void)viewDidLoad
{
// Setup your data source for the table
self.tableView.dataSource = self;
// Setup other stuff after loading the view
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1; // Return the number of sections you want in the table view
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows for each section, in your case this would be something like this
return self.posts.count; // If your data is stored in an array
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"YourCustomCell";
YourCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
if (!cell){
cell = [[YourCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
// Configure the cell...
// Here you add the code to display the data in the cell e.g.
[cell.profileImageView setImage:yourImageToDisplay];
return cell;
}
#end
As the comment said, you'd better choose UITableView as the posts' container and inherit from UITableViewCell as your box view.
Detailedly speaking much more, suppose the class inherited from UITableViewCell named MyTableViewCell, and you treat its contentView as your UIView *box. After that, you could tell the UITableView to use your MyTableViewCell as the Cell by sending him registerClass:forCellReuseIdentifier:.
Then, you can get MyTableViewCell instance by sending UITableView the
dequeueReusableCellWithIdentifier:forIndexPath: message. After being bound data (telling him display what posts), this instance can be returned to UITableView, which will arrange everything for you. Besides, these usually should be done in the UITableView's dataSource method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath.

Reveal view when swiping a prototype table view cell

What I am trying to do is reveal a hidden menu when swiping on a UITableViewCell.
I have no idea how to do it, any guidance will be appreciated.
Here is an image so that you get an idea:
Image http://imageshack.com/a/img855/9134/j6k8.png
u can do it by using custom cell i give the ruff idea and sample code how to achieve this,
first create a new file objective-c, and name it as CustomCell subclass of UITableViewCell and in CustomCell.h file
i took some dummy views and labels
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell
{
}
#property (nonatomic, retain) UIView *optionsView; //this is your options to show when swiped
#property (nonatomic, retain) UIView *mainVIew; //this is your view by default present on top of options view
#property (nonatomic, retain) UILabel *messageLabel;
#property (nonatomic, retain) UILabel *optionsLabel;
#end
in CustomCell.m
#import "CustomCell.h"
#implementation CustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//since we are not using the xib we hav to initilise hear
_optionsView = [[UIView alloc]init];
_mainVIew = [[UIView alloc]init];
_optionsView.backgroundColor = [UIColor greenColor];
_mainVIew.backgroundColor = [UIColor redColor];
_optionsView.alpha = 0.0f;
_mainVIew.alpha = 1.0f;
_messageLabel = [[UILabel alloc]init];
_optionsLabel = [[UILabel alloc] init];
[_optionsView addSubview:_optionsLabel]; //hear u can add image view or buttons to options view i just added the label
[_mainVIew addSubview:_messageLabel];
//add the gesture to main view
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(whenSwiped:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
[_mainVIew addGestureRecognizer:swipeGesture];//add it to main view
UISwipeGestureRecognizer *swipeReverse = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(whenSwipedReversed:)];
swipeReverse.direction = UISwipeGestureRecognizerDirectionLeft;
[_optionsView addGestureRecognizer:swipeReverse]; //add it to options view so that user can swipe back
[self.contentView addSubview:_optionsView]; //first add the optionsVIew
[self.contentView addSubview:_mainVIew]; //then main view
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)layoutSubviews
{
[super layoutSubviews];
_optionsView.frame = self.bounds;
_mainVIew.frame = self.bounds;
_messageLabel.frame = _mainVIew.bounds;
_optionsLabel.frame = _optionsView.bounds;
_optionsView.alpha = 0.0f;
_mainVIew.alpha = 1.0f;
}
//handle swipe call backs in cell only and make a delegate to controller about the button actions of options view
- (void)whenSwiped:(id)sender
{
CGRect frameRect = _mainVIew.frame;
frameRect.origin.x = 300.0f;
[UIView animateWithDuration:0.5 animations:^{
_mainVIew.frame = frameRect;
_optionsView.alpha = 1.0f;
}];
}
- (void)whenSwipedReversed:(id)sender
{
CGRect frameRect = _mainVIew.frame;
frameRect.origin.x = 0.0f;
[UIView animateWithDuration:0.5 animations:^{
_mainVIew.frame = frameRect;
_optionsView.alpha = 0.0f;
}];
}
#end
in view controller import #import "CustomCell.h"
in datasource method of tableview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *Cell = [tableView dequeueReusableCellWithIdentifier:#"CELL"];
if(Cell == nil)
{
Cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CELL"];
}
Cell.selectionStyle = UITableViewCellSelectionStyleNone;
Cell.optionsLabel.text = #"options";
Cell.messageLabel.text = #"swipe me";
return Cell;
}
above is example, hope this helps u .. :)
and comment if don't get
Here is some guidance:
Each UITableViewCell could have two views, one on top of the other. The top-most view is what is shown by default, and the bottom-most view is what you call the "hidden menu."
You will need to register a UISwipeGestureRecognizer in your storyboard that manages the table cells you are showing above. You will create an IBAction for that gesture recognizer in your custom UITableViewCell class. In that action, you will handle the swipes that occur and then displace the top-most view of the cell by your desire amount, up to a maximum displacement (in the x-direction).
If you need more than this, let me know and I'll provide more info. I can't tell from your original question just how experienced you are, e.g., do you know how to create UITableViews with custom UITableViewCells?
UPDATE:
Make sure that you are creating a XIB file that houses your custom UITableViewCell. Then you can easily add the UISwipeGestureRecognizer to the XIB, and connect it to an IBAction in your cell class. In your UIViewController, you will register the XIB with a reuse identifier and populate your UITableView this way.

UIButton inside UITableViewCell steals touch from UITableView

I have a problem similar to this one but answer provided there doesn't help much.
I have UITableView with some custom UITableViewCells, those cells have some nested custom UIViews and finally some UIButtons inside. The problem, as in question specified above, is that when I touch my button touch event won't populate to UITableView and it never scrolls.
Here's some code (it's just fastest way to reproduce, it's not my actual code):
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (strong, nonatomic) IBOutlet UITableView * tableView;
#end
#implementation ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
{
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,
self.view.bounds.size.width,
self.view.bounds.size.height)
style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [[UITableViewCell alloc] init];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor redColor];
button.frame = CGRectMake(0, 0, 50, 44);
[cell.contentView addSubview:button];
return cell;
}
#end
Red button in the only cell won't let table view to bounce scroll.
Can anyone help me a bit?
P.S. Don't pay attention to the stupidity of the code I've provided, I'm aware of all issues about it. I just provided it to show what the issue is all about. It's the conflict of buttonView and scrollView.
You can change the behaviour by overriding touchesShouldCancelInContentView: in the UITableView. For this to work you'll need to replace the table view with this subclass, either in loadView or in your xib file.
#interface AutoCancelTableView: UITableView
#end
#implementation AutoCancelTableView
//
// Overriding touchesShouldCanceInContentView changes the behaviour
// of button selection in a table view to act like cell selection -
// dragging while clicking a button will cancel the click and allow
// the scrolling to occur
//
- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
return YES;
}
#end
This is the standard behaviour of UIScrollView which tableviews use. The system doesn't know that you want to scroll until you move your finger, but by that time you have already "pressed" on the button and so it assumes that's what you want to do.
You can play with a couple of properties on your tableview's scrollview to change the behaviour, but you may find they negatively impact the feel of your cells and buttons because of added delays.
self.tableView.delaysContentTouches = YES;
delaysContentTouches
If the value of this property is YES, the scroll view delays handling the touch-down gesture until it can determine if scrolling is the intent...
and
self.tableView.canCancelContentTouches = YES
canCancelContentTouches
If the value of this property is YES and a view in the content has begun tracking a finger touching it, and if the user drags the finger enough to initiate a scroll, the view receives a touchesCancelled:withEvent: message and the scroll view handles the touch as a scroll.
Because all the above answers not working for me. I add a imageView on the button and make the imageView user interface YES, then add a tap gesture on the iamgeView. In the tap gesture related methods I put the button related methods in. so all is well..
may be hack. But it work well..
Below code works for me:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell = [tableView dequeueReusableCellWithIdentifier:#"cell_id"];
cell.userInteractionEnabled = YES;
if (cell == nil) {
cell = [[[CustomCell1 alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell_id"]autorelease];
}
[cell.button addTarget:self action:#selector(button_action) forControlEvents:UIControlEventTouchUpInside];}
-(void)button_action{NSLog(#"Hello!");}
This is my custom cell:
#import "CustomCell.h"
#implementation CustomCell
#synthesize button;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
button = [[UIButton alloc]init];
button =[UIColor redColor];
[self.contentView addSubview: button];
}
return self;}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+50 ,+15, 100, 100);
button = frame;}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{ [super setSelected:selected animated:animated];
// Configure the view for the selected state
}

Resources