UITableView, clicks, mimic button pressed - ios

I am working with UITableview, I want the cells to look like a pressed button, how can I do that?

try this ,
// get selected tableview cell and perform some animation on cell. i am using scale up and down animation
#pragma mark Table View Delgate method
- (void)tableView:(UITableView *)tbView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCellForTradeList *cell=(CustomTableViewCellForTradeList *)[tbl_TradeList cellForRowAtIndexPath:indexPath ];
cell.transform = CGAffineTransformMakeScale(0.85,0.85);
[self performSelector:#selector(NavigateToTradeBuySellAfterScaling:) withObject:indexPath afterDelay:0.1];
}
// rescale uitableview cell.
-(void)NavigateToTradeBuySellAfterScaling:(NSIndexPath *)indexPath
{
CustomTableViewCellForTradeList *cell=(CustomTableViewCellForTradeList *)[tbl_TradeList cellForRowAtIndexPath:indexPath ];
cell.transform = CGAffineTransformMakeScale(1.0,1.0);
[self performSelector:#selector(NavigateToTradeBuySellAfterScaling1:) withObject:indexPath afterDelay:0.1];
}
//navigate to other view controller
-(void)NavigateToTradeBuySellAfterScaling1:(NSIndexPath *)indexPath
{
TradeBuySellViewController* tradeBuySellController = [[TradeBuySellViewController alloc] initWithNibName:#"TradeBuySellViewController" bundle:nil :entity];
[self.navigationController pushViewController:tradeBuySellController animated:YES];
}

You can try changing the background view of UITableViewCell when clicked. You can get the clicked cell with this method.
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];

Related

Show/Hide custom class elements when uitableviewcell is selcted

I am new to iOS development. I have doubt related to UITableViewCell height.
I am using one tableview, I have taken prototype cell in storyboard. The cell height I have given is 75. But what I need is when I select cell height will be increase and show 1 button (which is in black color in the image). The button is declared in UITableViewCell's custom class. Below of that image I have one label. If button is showing I need to place the label at the bottom of button or else in the place of button I need to show that label.
Thanks in advance.......
You can use [tableView beginUpdates] and [tableView endUpdates];
for Ex:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self hideControlMethodsTable];//Declare method to hide when button tableview loads initially
[tableList beginUpdates];
[tableList deselectRowAtIndexPath:indexPath animated:YES];
CellRes = (ReservationTableViewCell*)
[tableList cellForRowAtIndexPath:indexPath];
if (cellSelectedFirstTime == true) {
self.selectedRow = indexPath.row;
[self showControlsMethodsTable];
cellSelectedFirstTime = false;
}
else{
if(self.selectedRow == indexPath.row){
self.selectedRow = -1;
[self hideControlMethodsTable];
}
else{
self.selectedRow = indexPath.row;
[self showControlsMethodsTable];
}
}
[tableList endUpdates];
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(openForFirstCell == true){
if (cellSelectedFirstTime==true) {
CellRes = [UIColor lightGrayColor];
return 180.;
}
return 60;
}
}
Hope this helps you.

SWTableViewCell slide programmatically

How can i slide programmatically SWTableViewCell.
//When i click in a cell i want to open the swtableviewcell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
???
}
i have tried:
[[[SWTableViewCell alloc]init]didTransitionToState:2];
and in swtableviewcell.m
-(void)didTransitionToState:(UITableViewCellStateMask)state{
NSLog(#"state: %lu",state);
if (_cellState == kCellStateCenter)
{
[self.cellScrollView setContentOffset:[self contentOffsetForCellState:kCellStateRight] animated:YES];
[self.delegate swipeableTableViewCell:self scrollingToState:kCellStateRight];
}
}
but is not correct. Can you help me?
Thanks,
Mikel
I wanted to be able to show the utility buttons when a table view cell was selected so I added a method in SWTableViewCell.m
-(void)showUtilityButtonsAnimated:(BOOL)animated {
// Force the scroll back to run on the main thread because of weird scroll view bugs
dispatch_async(dispatch_get_main_queue(), ^{
[self.cellScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
});
_cellState = kCellStateLeft;
if ([self.delegate respondsToSelector:#selector(swipeableTableViewCell:scrollingToState:)])
{
[self.delegate swipeableTableViewCell:self scrollingToState:kCellStateCenter];
}
}
and don't forget to add it to SWTableViewCell.h
- (void)showUtilityButtonsAnimated:(BOOL)animated;
Then call that method from within your -tableView:didSelectRowAtIndexPath: delegate method. For example:
if (indexPath.row == 0) {
SWTableViewCell *cell = (SWTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
[cell showUtilityButtonsAnimated:YES];
}

Contact App - Search and apply Selection Style

[Edited Post]
For practicing, I developing a contact app without any backend. I followed only one NSMutableArray for maintaining contacts which is displayed in a UITableView.
Objective;
Like to search a contact (Ex: Dad), user given a name in a textfield and touch up a button
In the button action, check if Dad contain in the Mutable array which one i followed.
If present, then it definitely present in the tableView as a row. So i want to put selectionStyle for that row.
If search text changes means (Ex: Mom), then revert previous selectionStyle and apply to the row(Mom).
If not present, label notify "Not Present" as text. (Done!).
IBAction:
-(IBAction)actionSearchNameInTable:(id)sender{
if([myContactsArray containsObject:txtForContactName.text]){
myTable.tag=5;
NSInteger tempIndex=[myContactsArray indexOfObject:txtForContactName.text];
NSIndexPath *tempIndexPath=[NSIndexPath indexPathForRow:tempIndex inSection:0];
[self tableView:myTable didSelectRowAtIndexPath:tempIndexPath]; //broke here
NSLog(#"Call Passed!");
}
}
didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(myTable.tag==5){
NSLog(#"called done!");
//Here i like to select that row and apply selectionStyle or favorite background color for that cell, idea?.
myTable.tag=0;
}
These are all just other part of my application. I sured that, Delegate and Datasource are connected properly.
You are calling didDeselectRowAtIndexPath and not didSelectRowAtIndexPath.
And I think you have not implemented didDeselectRowAtIndexPath in your class so thats the reason of crash.
and for your another question:
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(cell.tag==100)
{
cell.contentView.backgroundColor=[UIColor grayColor];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tblviw cellForRowAtIndexPath:indexPath];
cell.tag=100;
cell.contentView.backgroundColor=[UIColor grayColor];
}
Change this:
-(IBAction)actionSearchNameInTable:(id)sender{
if([myContactsArray containsObject:txtForContactName.text]){
myTable.tag=5;
NSInteger tempIndex=[myContactsArray indexOfObject:txtForContactName.text];
NSLog(#"TEmp index:%i",tempIndex);
NSIndexPath *tempIndexPath=[NSIndexPath indexPathForRow:tempIndex inSection:0];
NSLog(#"Temp IP:%#",tempIndexPath);
[self tableView:myTable didSelectRowAtIndexPath]; //broke here
NSLog(#"Call Passed!");
}
}
And for custom selection background do this in your cellForRowAtIndexPath.
UIView *customColorView = [[UIView alloc] init];
customColorView.backgroundColor = [UIColor colorWithRed:180/255.0
green:138/255.0
blue:171/255.0
alpha:0.5];
cell.selectedBackgroundView = customColorView;
Hope this helps.. :)
EDIT:
didSelectRowAtIndexPath is a delegate. It only get called when user interaction happens. I face a same problem some days ago. I did it in cellForRowAtIndexPath. For selecting a row do this:
if ([[myContactsArray objectAtIndex:indexPath.row] isEqualToString:txtForContactName.text]) {
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
Please try to see my edited answer...
-(IBAction)actionSearchNameInTable:(id)sender{
if([myContactsArray containsObject:txtForContactName.text]){
myTable.tag=5;
NSInteger tempIndex=[myContactsArray indexOfObject:txtForContactName.text];
NSLog(#"TEmp index:%i",tempIndex);
NSIndexPath *tempIndexPath=[NSIndexPath indexPathForRow:tempIndex inSection:0];
NSLog(#"Temp IP:%#",tempIndexPath);
[self tableView:myTable didSelectRowAtIndexPath:tempIndexPath]; //Change here......
NSLog(#"Call Passed!");
}
}
Action for searching that if given text is present or not:
-(IBAction)actionSearchNameInTable:(id)sender{
[txtForContactName resignFirstResponder];
if([myContactsArray containsObject:txtForContactName.text]){
myTable.tag=5;
[myTable reloadData];
}
}
In cellForRowAtIndexPath:
//Better delegate to apply selection style
//Code after dequeue...
myTableCell.textLabel.text=[myContactsArray objectAtIndex:indexPath.row];
myTableCell.imageView.image=[myContactsPhotosArray objectAtIndex:0];
{
myTableCell.textLabel.text=[myContactsArray objectAtIndex:indexPath.row];
myTableCell.imageView.image=[myContactsPhotosArray objectAtIndex:0];
if(myTable.tag==5){
NSLog(#"Ready to apply selectionStyle");
if ([[myContactsArray objectAtIndex:indexPath.row] isEqualToString:txtForContactName.text]) {
[myTable selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
myTable.tag=0;
}
}
return myTableCell;
}

scrollToRowAtIndexPath when Button inside UITableViewCell is pressed

I have a tableview with a few custom cells. Inside the first one is a button. When this button is pressed I want to scroll to a certain Section in my tableview. How do I have to link the button action with the tableview?
You can scroll to a certain section with using this function :
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated
the example of usage is :
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:5 inSection:indexPath.section]
atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
and for link the button action with tableview you can use protocol in you custom cell
You can make your cell's button a property and in cellForRowAtIndexPath you can set the target in the class that loads the table view so you won't need any delegates. Something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"YourCellIdentifier";
YourCustomCell *cell =[tableView dequeueReusableCellWithIdentifier:identifier];
if(cell == nil) {
cell = [YourCustomCell alloc/init method....
[cell.buttonProperty addTarget:self action:#selector(cellButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
}
//do other stuff with your cell
}
-(void)cellButtonTapped:(id)sender {
UIButton *button = (UIButton*)sender;
YourCustomCell *cell = (YourCustomCell*)button.superview.superview; //if the button is added to cell contentView or button.superview is added directly to the cell
NSIndexPath *path = [yourTableView indexPathForCell:cell];
[yourTableView scrollToRowAtIndexPath:path
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
}
Set up a delegation mechanism back from your cell - when creating the cell, assign it an NSIndexPath and pass it back from the cell when the button is tapped.
So in your UITableViewCell subclass you'll have:
- (IBAction)buttonPressed:(id)sender
{
[self.delegate buttonPressedOnCellAtIndexPath:cell.indexPath]
}
Back in the controller that's the delegate, respond to this method with:
- (void)buttonPressedOnCellAtIndexPath:(NSIndexPath)indexPath
{
[self.tableView scrollToRowAtIndexPath:indexPath];
}

In a UITableView, how do I know when scrollRectToVisible is complete for a row?

When I select a row in a UITableView, I'm calling scrollRectToVisible:animated on the GCRect of the row's frame, and immediately afterwards doing some other animations. My problem is that I don't know when the animation from scrollRectToVisible:animated is complete.
My code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRwoAtIndexPath:indexPath];
[self.tableView scrollRectToVisible:cell.frame animated:YES];
//more animations here, which I'd like to start only after the previous line is finished!
}
I ran across this UIScrollViewDelegate method:
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
// Do your stuff.
}
Only called for animated scrolls. Not called for touch-based scrolls. Seems to work great.
An easier way is to encapsulate the scrolling code in a UIView animateWith[...] block, like this:
[UIView animateWithDuration:0.3f animations:^{
[self.tableView scrollRectToVisible:cell.frame animated:NO];
} completion:^(BOOL finished) {
// Some completion code
}];
Note that animated == NO in the scrollRectToVisible:animated: method.
The protocol UITableViewDelegate conforms to UIScrollViewDelegate. You can set BOOL parameter when you scroll manually and than check it in scrollViewDidScroll:
BOOL manualScroll;
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRwoAtIndexPath:indexPath];
manualScroll = YES;
[self.tableView scrollRectToVisible:cell.frame animated:YES];
}
...
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (manualScroll)
{
manualScroll = NO;
//Do your staff
}
}
Don't forget to set UITableViewDelegate.

Resources