I want to hide a label of a cell in a tableview.
(void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer
{
//Get location of the swipe
CGPoint location = [gestureRecognizer locationInView:self.tableView];
//Get the corresponding index path within the table view
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
//Check if index path is valid
if(indexPath)
{
//Get the cell out of the table view
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
//Update the cell or model
displayLabel.hidden = TRUE;
[cell setNeedsDisplay];
}
}
This code is hiding the label in the last cell as I failed to specify the code to hide the swiped cell's label.
Help to specify the swiped cell label to hide.
displayLabel.hidden = TRUE;
I need a replacement for this code.
You can try below code its working perfectly on my side:
- (void)viewDidLoad
{
UISwipeGestureRecognizer *recog = [[UISwipeGestureRecognizer alloc]initWithTarget:self
action:#selector(handleSwipeRight:)];
recog.delegate = self;
[recog setDirection:UISwipeGestureRecognizerDirectionRight];
[testTable addGestureRecognizer:recog];
// add the swipe gesture recognizer to tableview;
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 7;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
UILabel *aLabel = [[[UILabel alloc]init]autorelease];
aLabel.frame = CGRectMake(5, 0, 100, 40);
aLabel.text = [NSString stringWithFormat:#"aLabel %d",indexPath.row+1];
aLabel.tag = 1;//tag the labels
[cell.contentView addSubview:aLabel];
UILabel *bLabel = [[[UILabel alloc]init]autorelease];
bLabel.frame = CGRectMake(110, 0, 100, 40);
bLabel.text = [NSString stringWithFormat:#"bLabel %d",indexPath.row+1];
bLabel.tag = 2;//tag the label
[cell.contentView addSubview:bLabel];
UILabel *cLabel = [[[UILabel alloc]init]autorelease];
cLabel.frame = CGRectMake(215, 0, 100, 40);
cLabel.text = [NSString stringWithFormat:#"cLabel %d",indexPath.row+1];
cLabel.tag = 3;//tag the label
[cell.contentView addSubview:cLabel];
}
return cell;
}
-(void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer
{
//Get location of the swipe
CGPoint location = [gestureRecognizer locationInView:testTable];
//Get the corresponding index path within the table view
NSIndexPath *indexPath = [testTable indexPathForRowAtPoint:location];
//Check if index path is valid
if(indexPath)
{
//Get the cell out of the table view
UITableViewCell *cell = [testTable cellForRowAtIndexPath:indexPath];
for (id label in cell.contentView.subviews)
{
if ([label isMemberOfClass:[UILabel class]])
{
UILabel *referedLabel = (UILabel*)label;
if (referedLabel.tag == 2) //tag of bLabel;
{
referedLabel.hidden = YES;
}
}
}
}
}
I think displayLabel is pointing to label in last cell of your table. Where you are setting value to displayLabel.
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
//Update the cell or model
//You should get label from cell here. Currently displayLabel may be pointing to label in last cell, if I am correct you are assigning value to displaylabel in cellForRowAtIndexPath or in any other method.
displayLabel.hidden = TRUE;
[cell setNeedsDisplay];
Change your method to this:
-(void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer
{
//Get location of the swipe
CGPoint location = [gestureRecognizer locationInView:self.tableView];
//Get the corresponding index path within the table view
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
//Check if index path is valid
if(indexPath)
{
// If you created a custom cell with a
// displayLabel property, change the pointer
// type from UITableViewCell to that type
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.displayLabel.hidden = YES;
// Use the following three lines instead, if your cell style is "Subtitle"
// cell.textLabel.hidden = YES;
// cell.imageView.hidden = YES;
// cell.detailTextLabel.hidden = YES;
[cell setNeedsDisplay];
}
}
Related
-(void)sendcomment:(UIButton *)sender{
NSLog(#" send commment server");
thirdviewcellTableViewCell *dja =[[thirdviewcellTableViewCell alloc] init];
NSString *vga = [[NSString alloc] init];
vga=dja.celltext;
NSLog(#"comment value is %#",vga);
NSLog(#"comment cell value is %#",dja.celltext);
}
-(void)sendcomment:(UIButton *)sender
{
UIButton *button = (UIButton*)sender;
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
UITableViewCell *cell = (UITableViewCell*)[tblView cellForRowAtIndexPath: indexpath];
// If you have text field
UITextField *textfiled = (UITextField *)[cell.contentView viewWithTag:yourtextfeildtagvlaue];
// NSString *vga = textfiled.text
}
If the cell hold the button, you can do this:
-(void)sendcomment:(UIButton *)sender{
// 1. get the position the button you clicked
CGPoint correctedPoint = [sender convertPoint:button.bounds.origin toView:self.tableView];
// 2. find the cell via the position
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:correctedPoint];
thirdviewcellTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];;
// 3. then you can get the TextField value
NSString *textFieldValue = [[NSString alloc] init];
textFieldValue = cell.celltext;
}
you get your cell using cell's hierarchy
-(void)sendcomment:(UIButton *)sender
{
UIButton *button = (UIButton*)sender;
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
UITableViewCell *cell = (UITableViewCell*)sender.superview.superview;// get cell using view hierarchy
// If you have text field
UITextField *textfiled = (UITextField *)[cell.contentView viewWithTag:yourtextfeildtagvlaue];
// NSString *vga = textfiled.text
}
*
In your action method you can Find Your TextField as below :
You can find IndexPath from SuperView.
See below Code.
UIView *superView = btn.superview;
while (![superView isKindOfClass:[UITableViewCell class]]) {
superView = superView.superview;
}
CustomCell *cell = (CustomCell *)superView;
You will get UITextField object from cell.yourTextFeld .
And you can use TextField as per your requirement.
In ViewDidLoad of table view controller, I first added SwipeGuesture and handler "Swipecell"
UISwipeGestureRecognizer *showExtrasSwipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(Swipecell:)];
showExtrasSwipe.direction = UISwipeGestureRecognizerDirectionRight|
UISwipeGestureRecognizerDirectionLeft;
[self.tableView addGestureRecognizer:showExtrasSwipe];
//Here I define the handler:
-(void)Swipecell:(UISwipeGestureRecognizer *)gesture
{
//Taking gesture location
CGPoint location = [gesture locationInView:_tableview];
//Getting IndexPath for that location in tableview
NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
NSString *data = [self.dataArray objectAtIndex:swipedIndexPath.row];
//Setting the label of the cell
[swipedCell.textLabel setText:data];
}
But it is working only for one first row. Can anybody help me in this or give me better solution? Don't forget to rate, if you like the question.
Please add the swipe gesture in cellForRowAtIndexPath method , so the gesture will be added to each cell.
It works... Tested on iOS 7.1 simulator
- (void)viewDidLoad
{
_tableview = (UITableView*)self.view;
[super viewDidLoad];
UISwipeGestureRecognizer *showExtrasSwipe = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(Swipecell:)];
showExtrasSwipe.direction = UISwipeGestureRecognizerDirectionRight|
UISwipeGestureRecognizerDirectionLeft;
[self.tableView addGestureRecognizer:showExtrasSwipe];
}
-(void)Swipecell:(UISwipeGestureRecognizer *)gesture
{
CGPoint location = [gesture locationInView:_tableview];
NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
swipedCell.detailTextLabel.text = [NSString stringWithFormat:#"%i", swipedIndexPath.row];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 55;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
}
cell.textLabel.text = [NSString stringWithFormat:#"%i", indexPath.row];
cell.detailTextLabel.text = #" ";
return cell;
}
Change [gesture locationInView:_tableview] to [gesture locationInView:self.tableview]. I was refering to tableView outlet.
I have this collapsable UITableView where there is a UITextField and a UIButton in the last cell in each table section. I would like to send the text in the UITextField to the function that is called by the UIButton that is next to it in the same cell, but I am baffled in how to achieve this. Thanks in advance for the help!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([self tableView:_tableView canCollapseSection:indexPath.section])
{
int num = 1;
if (self.chat[indexPath.section - 1][#"num"] != nil)
num = [self.chat[indexPath.section - 1][#"num"] intValue] + 1;
if (!indexPath.row)
{
cell.textLabel.text = self.chat[indexPath.section - 1][#"msg"]; // only top row showing
if ([expandedSections containsIndex:indexPath.section])
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeUp];
}
else
{
cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor grayColor] type:DTCustomColoredAccessoryTypeDown];
}
}
else if (indexPath.row < num && indexPath.row >= 1)
{
cell.textLabel.text = self.chat[indexPath.section - 1][key];
cell.accessoryView = nil;
}
else
{
///////////////////////////////////////////
////////This is the important part/////////
///////////////////////////////////////////
UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(14, 6, 245, 31)];
self.sendButton = [[UIButton alloc] initWithFrame:CGRectMake(265, 1, 50, 40)];
[self.sendButton setTitle:#"Reply" forState:UIControlStateNormal];
[self.sendButton setTitleColor:UIColorFromRGB(0x960f00) forState:UIControlStateNormal];
[cell addSubview:self.sendButton];
[self.sendButton addTarget:self action:#selector(sendReply:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:field];
cell.accessoryView = nil;
}
}
else
{
cell.accessoryView = nil;
cell.textLabel.text = #"Normal Cell";
}
return cell;
}
Give some unique tag to textfield and button by below way:
///////////////////////////////////////////
////////This is the important part/////////
///////////////////////////////////////////
UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(14, 6, 245, 31)];
self.sendButton = [[UIButton alloc] initWithFrame:CGRectMake(265, 1, 50, 40)];
[self.sendButton setTitle:#"Reply" forState:UIControlStateNormal];
[self.sendButton setTitleColor:UIColorFromRGB(0x960f00) forState:UIControlStateNormal];
self.sendButton.tag = indexPath.section *1000 + indexPath.row;
[cell addSubview:self.sendButton];
[self.sendButton addTarget:self action:#selector(sendReply:) forControlEvents:UIControlEventTouchUpInside];
field.tag = self.sendButton.tag + 1;
[cell addSubview:field];
cell.accessoryView = nil;
Now on button event,
-(void) sendReply:(UIButton *)sender
{
UITextField *field = [YOOUR_TABLE_VIEW viewWithTag:sender.tag + 1];
//Do you coding here
}
Make a Custom UITableViewCell that has uitextfield and a button on it and make a protocol/delegate of that custom uitableviewcell you've created.. so you can have more control of your code and the event of your button in the future..
check this tutorial: http://www.codigator.com/tutorials/ios-uitableview-tutorial-custom-cell-and-delegates/
cheers
For this,
In cellForRowAtIndexPath: method where you are allocating the send button add one line just to give some identifier to send button as below,
[self.sendButton setAccessibilityIdentifier:[NSString stringWithFormat:#"%d#%d",indexPath.row,indexPath.section]];
Now, in sendReply: method,
//First get the row and section information
NSString *str=[sender accessibilityIdentifier];
NSArray *arr=[str componentsSeparatedByString:#"#"];
//Get the index path
NSIndexPath *iRowId =[NSIndexPath indexPathForRow:[[arr objectAtIndex:0] intValue] inSection:[arr objectAtIndex:1] intValue]];
//get the cell
UITableViewCell *objCell=(UITableViewCell*)[tblviwBasketCell cellForRowAtIndexPath:iRowId];
Now objCell will be the cell in which you have added the button and text view. so get the subviews of objCell and access the textfield to get the text.
As you say, there is a text field in each section. You should set the tag of your UIButton and UITextField same as indexPath.section.
Then, in the target method, use this tag value to get the relevant cell from the relevant section, and iterate over it's subviews to get your textField.
In the target method, you should do something like this:
- (void) sendReply:(id)sender {
int section = [(UIButton*)sender tag];
int row = [myTableView numberOfRowsInSection:section] - 1;//assuming it is the last cell in the section that contains your textField.
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:section];
UITableViewCell* cell = [myTableView cellForRowAtIndexPath:indexPath];
for (UIView* vu in cell.subviews) {
if ([vu isKindOfClass:[UITextField class]]) {
NSString* textString = [(UITextField*)vu text];
//perform any tasks you want with the text now
}
}
}
You can simply use viewWithTag:, since it does an iterative search, but all the extra code is to avoid iterating over all the cells before reaching your relevant cell.
![enter image description here][2]I have a table view,in which I am using my custom cell that contains user's profile picture,status label, time stamp label, username label, no of comments label and a comment button.
When no of comments i.e"4 comments" is tapped, I ha ve to show comments in the corresponding cell along with username, user profile picture etc below the original post.
How do I dynamically add these objects into cell when the app is running? or there is some else solution please tell. table for 2 posts is shown in right of image. Tapping no of comments left of the image should be shown.
Here is the code for table cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MyCell";
TableCell *cell = (TableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"TableCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (TableCell *)currentObject;
break;
}
}
}
// Configure the cell.
User *user = [postTableData objectAtIndex:indexPath.row];
NSLog(#"pic:%#",user.profilePictureURL);
cell.profilePicture.image = [UIImage imageNamed:#"avatar.png"];
cell.profilePicture.layer.cornerRadius = cell.profilePicture.frame.size.height /2;
cell.profilePicture.layer.masksToBounds = YES;
cell.profilePicture.layer.borderWidth = 0;
cell.userName.text = user.name;
cell.status.text = user.status;
cell.dateForStatus.text = user.datePosted;
[cell.countComments setTitle:user.countOfComments forState:UIControlStateNormal];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.contentView.layer setBorderWidth:6.0f];
[cell.contentView.layer setBorderColor:[[UIColor alloc] initWithRed:233/255.0 green:236/255.0 blue:233/255.0 alpha:1.0].CGColor];
cell.commentButton.tag=indexPath.row;
[cell.commentButton addTarget:self action:#selector(commentButtonPressAction:) forControlEvents:UIControlEventTouchUpInside];
return cell;
![enter image description here][3]}
I guess you are trying to expand the cell.
You can call these few lines to update the cell, including the height.
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];
So just set a flag to show comments in cellForRowAtIndexPath.
//AFTER_YOUR_SETTING
if(showComment){
[cell loadCommentMethod];
}
And also, in heightForRowAtIndexPath
if(showComment){
return [CellClass publicMethodForCalHeight:dataAtIndexPath];
} else {
return kDefaultCellHeight;
}
Change your button action method:
- (void)onButtonTaped:(id)sender
{
UIButton *button = sender;
UILabel *newLabel = [ [ UILabel alloc] initWithFrame:CGRectMake (0, 100, 200, 50)];
newLabel.text = #"Your new text here";
[button.superview addSubview:newLabel];
}
But label will be visible only until cell will be visible on screen. When cell will be recreated label will be deleted. To keep label you still need to change
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I'm not sure if it works but you can try this. Override touchesBegan: on your cell:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
CGRect rect = [noComments frame];
if (CGRectContainsPoint(rect, point))
{
UILabel *label = ... // the label you need to add.
[self addSubview:label];
}
}
You can make use of the method insertRowsAtIndexPaths:withRowAnimation: of the UITableView. You can refer this link for a tutorial. Also refer the Apple docs
I currently have a custom UITableViewCell which contains a UIImageView and trying to add a UITapGestureRecognizer on the UIImageView with no luck. here is snippet of the code.
//within cellForRowAtIndexPath (where customer table cell with imageview is created and reused)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleImageTap:)];
tap.cancelsTouchesInView = YES;
tap.numberOfTapsRequired = 1;
tap.delegate = self;
[imageView addGestureRecognizer:tap];
[tap release];
// handle method
- (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer {
RKLogDebug(#"imaged tab");
}
I've also set userInteractionEnabled on the cell and the superview of the UIImageView but still no luck, any hints?
EDIT:
I've also remove cell's selection by cell.selectionStyle = UITableViewCellSelectionStyleNone; Could this be a problem
UIImageView's user interaction is disabled by default. You have to enable it explicitly to make it respond to touches.
imageView.userInteractionEnabled = YES;
Swift 3
This worked for me:
self.isUserInteractionEnabled = true
In my case it looks like :
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = CELL_ROUTE_IDENTIFIER;
RouteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[RouteTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
if ([self.routes count] > 0) {
Route *route = [self.routes objectAtIndex:indexPath.row];
UITapGestureRecognizer *singleTapOwner = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(imageOwnerTapped:)];
singleTapOwner.numberOfTapsRequired = 1;
singleTapOwner.cancelsTouchesInView = YES;
[cell.ownerImageView setUserInteractionEnabled:YES];
[cell.ownerImageView addGestureRecognizer:singleTapOwner];
} else {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
And selector :
- (void)imageOwnerTapped:(UISwipeGestureRecognizer *)gesture {
CGPoint location = [gesture locationInView:self.tableView];
NSIndexPath *tapedIndexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell *tapedCell = [self.tableView cellForRowAtIndexPath:tapedIndexPath];
NSIndexPath *indexPath = [self.tableView indexPathForCell:tapedCell];
NSUInteger index = [indexPath row];
Route *route = [self.routes objectAtIndex:index];
}