Select a UITableViewCell programmatically if it contains cgrect - ios

I have a UITableView. I have an image like an arrow beside the tableview.
I have to check whether the arrow is intersecting with a particular cell so that I can programatically select that cell. How do I do this?
I dont have a problem in the type of selection of the cell/row but having problems detecting whether the arrow is on a particular row.
I have written this code but not working :
NSArray *paths = [tableView indexPathsForVisibleRows];
for (NSIndexPath *path in paths)
{
CGRect myRect = [tableView rectForRowAtIndexPath:indexPath];
CGRect myRect1 = CGRectMake(-12, 234, 55, 52);
if (CGRectIntersectsRect(myRect1, myRect))
{
// Also tried [tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
cell.selected = YES;
NSLog(#"\n \n \n myrect = %# myrect1 = %# , index = %# \n \n ", NSStringFromCGRect(myRect),NSStringFromCGRect(myRect1), indexPath);
}
else
{
cell.selected = NO;
}
}
Images shows what I want to achieve and not whats existing.

Ok...for the problem with the arrow... I think the table is returning the rectangle for each row IN REGARDS to the table's frame. So I think you need to do this:
for (NSIndexPath *path in paths)
{
CGRect myRect = [tableView rectForRowAtIndexPath:indexPath];
CGRect myRect1 = CGRectMake(-12, 234, 55, 52);
myRect.origin.x += tableView.frame.origin.x;
myRect.origin.y += tableView.frame.origin.y;
if (CGRectIntersectsRect(myRect1, myRect))
{
//selection code here
}
else
{
// whatever you want
}
}

I have found the answer.. Posting the code
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
NSArray *paths = [tableView indexPathsForVisibleRows];
for (NSIndexPath *indexPath in paths)
{
CGRect myRect = [tableView convertRect:[tableView rectForRowAtIndexPath:indexPath] toView:[tableView superview]];
CGRect myRect1 = CGRectMake(-12, 236, 20, 30);
if (CGRectIntersectsRect(myRect1, myRect) && notSelected == FALSE)
{
notSelected = TRUE;
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
else
{
notSelected = FALSE;
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
}
}

Related

UITableView mess up after resizing | Objective-C

I'm using a TableView to create a chat app. Where each cell represent a msg. The cell height change based on the contents of the msg.
For example if its a photo it will have a fixed size.
If its a text it will have a size depend of number of line in msg.
Also the whole TableView change size when the user try to send a msg based on the size of the keyBoard appeared.
The problem is that when the tableView size changes, the tableView cells get messed up!
This only happen when is scroll down (re-render the cells that disappear)
Update: This happened also if some of the cells where invisible (big tableView). before the re-sizing. And also it does not have anything with the type. Because it does the same if the msgs are all text
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([[[msg objectAtIndex:indexPath.row] objectForKey:#"type"] isEqualToString:#"msg"]) {
int numberOfline= [[[msg objectAtIndex:indexPath.row] objectForKey:#"line"] intValue];
return topMergeH + topBubbleH + bottomBubbleH + bottomMergeH + (bubbleHieght*numberOfline);
}
else
return topMergeH + topBubbleH + bottomBubbleH + bottomMergeH + bubbleHieght + 220;
}
- (void) changeTextFieldPoistion {
//TextFieldHight = 30
[_typeRef setFrame:
CGRectMake(0,self.view.frame.size.height-keyBoardHeight-30
, _typeRef.frame.size.width, _typeRef.frame.size.height)];
[_myTableView setFrame:
CGRectMake(_myTableView.frame.origin.x, _myTableView.frame.origin.y
, 374, self.view.frame.size.height-_myTableView.frame.origin.y-keyBoardHeight-30)];
}
Pic1: Before Re-sizing
Pic2: After Re-sizing 1
I found the answer here
The problem was with the Cell Identifier
Before Code:
static NSString *simpleTableIdentifier = #"ChatSendTableViewCell";
ChatSendTableViewCell *cell =
(ChatSendTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:simpleTableIdentifier owner:self options:nil];
cell = [nib objectAtIndex:0];
}
After Code:
static NSString *CellIdentifier1 = #"ChatSendTableViewCell";
UITableViewCell *cell;
if (indexPath.section == 0)
{
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier1];
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
}
}
1 - when keyboard Appers Get keyboard size -
keyboardInfo = [notification userInfo];
kbSize = [[keyboardInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
2-if your chatTable Contains Any row -
whwre [messageList] is Number of Rows
if ([messageList count]) {
NSIndexPath *iPath = [NSIndexPath indexPathForRow:[messageList count]-1 inSection:0];
UITableViewCell *lastCell = [chatTable cellForRowAtIndexPath:iPath];
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, lastCell.frame.origin) ) {
CGRect bkgndRect = lastCell.superview.frame;
bkgndRect.size.height += kbSize.height;
[lastCell.superview setFrame:bkgndRect];
[chatTable setContentOffset:CGPointMake(0.0,(lastCell.frame.origin.y+lastCell.frame.size.height) - (kbSize.height)) animated:NO];
}
}

Set UITableViewCell in full screen

I want to set UITableViewCell in full screen when user select cell with bouncing animation. I shared link below. I want same like this.
https://drive.google.com/open?id=0B9k_Shyb5v62eFdxWXhYeXV3a0E
I set view inside cell. But I can't set it in full screen with animation.
Below is my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableViewList cellForRowAtIndexPath:indexPath];
int index = [cell.accessibilityIdentifier intValue];
CGFloat delay = 0.0f;
if (indexSelected == (int)indexPath.row)
{
//COLLAPSE
indexSelected = -1111111111;
UIView *viewController = (UIView *)[cell.contentView viewWithTag:TAG_DETAIL_CONTROLLER];
[viewController removeFromSuperview];
tableViewList.frame = frameTableViewList;
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
btnBack.hidden = YES;
indexSelected = -1;
}
else
{
//EXPAND
if (indexSelected < 0)
indexSelected = index;
else
{
int indexPrevious = indexSelected;
indexSelected = (int)indexPath.row;
[tableView reloadRowsAtIndexPaths:#[[NSIndexPath indexPathForRow:indexPrevious inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
delay = 0.3;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
CGRect frame = tableViewList.frame;
frame.origin.y = 0;
frame.size.height = CGRectGetHeight(self.view.frame);
tableViewList.frame = frame;
btnBack.hidden = NO;
});
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
cell = [self reuseTableViewCellWithIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIView *viewController = (UIView *)[cell.contentView viewWithTag:TAG_DETAIL_CONTROLLER];
if (indexSelected >= 0 && indexSelected == (int)indexPath.row)
[self addDiamondDetailView:cell index:(int)indexPath.row];
else
{
if (indexSelected == -1111111111)
{
CGRect frame = cell.frame;
frame.size.height = cellHeight;
cell.frame = frame;
cell.contentView.frame = CGRectMake(0, 0, CGRectGetWidth(cell.frame), CGRectGetHeight(cell.frame));
}
[viewController removeFromSuperview];
}
cell.contentView.clipsToBounds = YES;
return cell;
}
- (void)addDiamondDetailView:(UITableViewCell *)cell index:(int)index
{
DetailVC = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
DetailVC.selectedStoneInfo = (StoneInfo *)[arrStone objectAtIndex:index];
DetailVC._STONE_DETAIL_TYPE = self._RESULT_PAGE_TYPE == RESULT_MY_CART ? STONE_DETAIL_CART : STONE_DETAIL_RESULT;
DetailVC.delegate = self;
DetailVC.view.tag = TAG_DETAIL_CONTROLLER;
[cell.contentView addSubview:stoneDetailVC.view];
//Cell Frame
CGRect frame = cell.bounds;
frame.size.height = cellDetailHeight;
cell.frame = frame;
cell.contentView.frame = CGRectMake(0, 0, CGRectGetWidth(cell.frame), CGRectGetHeight(cell.frame));
//Stone Detail VC Frame
frame = stoneDetailVC.view.frame;
DetailVC.view.frame = CGRectMake(0, 0, CGRectGetWidth(stoneDetailVC.view.frame), cellDetailHeight);
}
I've added ViewController into cell as sub view. I want to set table cell in view full screen with bounce animation. Please help me.

How to simultaneously animate a cell and table change in iOS?

I am trying to simultaneously animate a custom UITableViewCell and the UITableView that contains it.
When clicked, the subviews of the UITableViewCell slightly re-arrange such that the height of the cell increases. I want the re-arrangement within the cell to happen concurrently with the resizing of the UITableView's slot for that cell, so that it doesn't overlap with the cell below it.
When clicked again, the reverse is to happen.
This is what I've tried so far.
The UITableView
When the UITableView is clicked, I use didSelectRowAtIndexPath to update a selectedPath property:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.selectedPath && indexPath.row == self.selectedPath.row) {
self.selectedPath = nil;
} else {
self.selectedPath = indexPath;
}
[tableView reloadData];
}
Note, it calls reloadData, which triggers each visible cell to re-fetch the prototype and configure it based on whether it is selected or not:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
QueueItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"QueueCell" forIndexPath:indexPath];
[cell setImage:[UIImage imageNamed:[self.imageList objectAtIndex:indexPath.row]]];
if (self.selectedPath && indexPath.row == self.selectedPath.row) {
[cell makeSelected:YES];
} else {
[cell makeSelected:NO];
}
return cell;
}
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*) indexPath {
CGFloat height = 210;
if (indexPath.row == [self.imageList count] - 1) {
height += 10;
}
if (self.selectedPath && indexPath.row == self.selectedPath.row) {
height += 35;
}
return height;
}
The UITableViewCell subclass
In the custom cell class, its animation happens when it is made selected or not selected:
- (void)makeSelected:(BOOL)selected {
if (selected) {
[UIView animateWithDuration:0.5 animations:^{
self.customImage.frame = CGRectMake(5, 5, 310, 210);
}];
} else {
[UIView animateWithDuration:0.5 animations:^{
self.customImage.frame = CGRectMake(10, 10, 300, 200);
}];
}
}
What is happening
The table view immediately snaps to the new the allotted height for the cell, and then the cell's contents slowly animate to the new state. I want the two things to happen concurrently, and smoothly.
You might want to call
[UIView animateWithDuration:0.5 animations:^{
[self.tableView reloadData];
}];
and re-write the custom cell as following:
- (void)makeSelected:(BOOL)selected {
if (selected) {
self.customImage.frame = CGRectMake(5, 5, 310, 210);
} else {
self.customImage.frame = CGRectMake(10, 10, 300, 200);
}
}
I hope it helps.
try this... reload particular cell insteade
NSIndexPath* indexPath1 = [NSIndexPath indexPathForRow:selectedRowIndexPath inSection:section];
// Add them in an index path array
NSArray* indexArray = [NSArray arrayWithObjects:indexPath1, nil];
// Launch reload for the two index path
[self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade];
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
You need to reload particular cell while tap use like this..
- (void)makeSelected:(BOOL)selected {
if (selected) {
self.customImage.frame = CGRectMake(5, 5, 310, 210);
} else {
self.customImage.frame = CGRectMake(10, 10, 300, 200);
}
}
Declare BOOl isSelect; as Global
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.selectedPath && indexPath.row == self.selectedPath.row) {
isSelect = NO;
} else {
self.selectedPath = indexPath;
isSelect = YES;
}
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
QueueItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"QueueCell" forIndexPath:indexPath];
[cell setImage:[UIImage imageNamed:[self.imageList objectAtIndex:indexPath.row]]];
if (self.selectedPath && indexPath.row == self.selectedPath.row && isSelect) {
[cell makeSelected:YES];
} else {
[cell makeSelected:NO];
}
return cell;
}
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*) indexPath {
CGFloat height = 210;
if (indexPath.row == [self.imageList count] - 1) {
height += 10;
}
if (self.selectedPath && indexPath.row == self.selectedPath.row && isSelect) {
height += 35;
}
return height;
}
It reload the cell with animation.. I hope it will help you.. Now you check it will work as you want...

UITableViewCell & getting bounds/frame

I have UITableView cell, in a UITableView. When i am trying to add new value from search bar list selected that time that value i am adding into table and same time displying the popover on that newly added cell but some time its working perfect but no of times it display anywhere i am using cell.frame my code is bellow for the same i am attach the expected result screen short also please any one know then help me
- (void)loadPopOver:(id)idDataObj {
NSDictionary *dictDataObj = (NSDictionary*)idDataObj;
//add the popOverControl
OrdersDetailPopViewController *orderDetailPopVC = [[OrdersDetailPopViewController alloc] init];
orderDetailPopVC.idParentDelegate = self;
CGSize sizeObj = [orderDetailPopVC initView:5 totalTextArea:0 dataForTableViews:[dictDataObj valueForKey:TAG_DATASOURCE] titles:[dictDataObj valueForKey:TAG_TITLES]];
if (sizeObj.height == 0 && sizeObj.width == 0) {
NSLog(#"API Error : Incomplete data sent.");
return;
}
//Point from where to show the popOverControl
CGRect cgRect;
if (indexPathLastSelected.section == -1 ) {
if([_isFromSearchBar isEqualToString:#"FROM_SEARCH_BAR"])
{
UITableViewCell *cell = [tableViewLeft cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
cgRect = cell.frame;
//cgRect.origin.y = 190;
cgRect.origin.y = 25;
//cgRect.origin.x =270;
}
else if([_isFromRightTable isEqualToString:#"FROM_RIGHT_TABLE"])
{
UITableViewCell *cell = [tableViewLeft cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
cgRect = cell.frame;
}
}
else {
UITableViewCell *cell = [tableViewLeft cellForRowAtIndexPath:indexPathLastSelected];
cgRect = cell.frame;
}
popOverFormInput = [[UIPopoverController alloc] initWithContentViewController:orderDetailPopVC];
popOverFormInput.contentViewController = orderDetailPopVC;
[popOverFormInput setPopoverContentSize:sizeObj];
popOverFormInput.delegate = self;
if (self.view.window != nil)
[popOverFormInput presentPopoverFromRect:cgRect inView:tableViewLeft permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
You have a lot of funky code in your project which I highly suggest you revise. For now though, I suggest going for simplicity and using a method like this
//Get the cell's frame that you're interested in
UITableViewCell *cell = [tableViewLeft cellForRowAtIndexPath:selectedindexPath]
//and then use that cell's frame to position your popover over it.
[popOverFormInput presentPopoverFromRect:[cell frame] inView:cell.contentView
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
As you are trying to popup using the CGRect, It might not the superView's frame Or else you are somewhere misusing of the frame Value.
Better get(Note) the NSIndePath of the tableView. Later can use it to popup from it >
For Ex:
- (void)loadPopOver:(id)idDataObj {
NSIndexPath *selectedindexPath;
.
.
.
if([_isFromSearchBar isEqualToString:#"FROM_SEARCH_BAR"])
selectedindexPath = // [tableViewLeft cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
.
.
.
// Try this to pop your View
[self.popOver presentPopoverFromRect:[tableViewLeft rectForRowAtIndexPath:selectedindexPath] inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
At the time of calling method i just put the delay
and i fixed the issue
[self performSelector:#selector(showPopOver) withObject:nil afterDelay:0.20];

Hide a label of a cell in a table view in iOS

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];
}
}

Resources