I'm using Objective-C. Here I have a tableview, and I set some actions using this method:
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
So when I slide the cell, it looks like this: the cell
I want to dismiss these action buttons after I clicked on one of them. Which method should I use?
These code will do well:
[tableView setEditing:NO animated:YES];
Related
I an doing a table view cell auto selecting for the first time by using
[cell setSelected:YES animated:YES];
It select the cell programmatically for the first time. It works fine. Now I want to deselect that cell when i select another cell in the meantime. how can i do that. I know when i select a cell then tableview delegate method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
is called and when deselect a cell then "did deselect row at index path" is called.
But the problem is i select the cell programmatically and when i select another cell then delegate method
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
doesn't call.
What should i do to solve this problem or what will be the logic to solve that problem?
Instead of calling setSelected:animated: on the cell directly, call the selectRowAtIndexPath:animated:scrollPosition: method of the table view to set the initial selection.
You can use below method to deselect row:
[tableView deselectRowAtIndexPath:indexPath animated:YES];
You just need to stored index when it's selected. So you just need to check if you have selected indexpath then make it deselect.
Take an object of NSIndexPath:
NSIndexPath *currentSelectedPath;
Update your didSelectRow method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(currentSelectedPath)
{
[tableView deselectRowAtIndexPath:currentSelectedPath animated:YES];
}
currentSelectedPath = indexPath;
// Do your other stuff
}
My UITableView shows answers for questions.
To switch questions I apply CATransition to tableView and call reloadData method.
In case if user did select one row, went to another question and then returned back to the first question, I want the chosen answer to be selected.
I call
[tableView selectRowAtIndexPath:indexPath
animated:YES
scrollPosition:UITableViewScrollPositionNone];
in delegate method
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
But when answers appear after reloading UITableView, there are no selected cells. If I move cell out of the screen and then move it back, it becomes selected.
you need to implement following UITableView delegate method to perform any action on selection of cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// [self doSomethingWithRowAtIndexPath:indexPath];
// write your action on cell here
}
I have just added a UITableView in to my storyboard and the suggested constraints to it like that.. Now i have written very simple code with some delegate and datasource methods implemented like this. . Now when i tap the first row it detects nothing and check the console in the below image, when i press the second row it tells me that the indexPath.row is 0.. can some body point out that what i am doing wrong here.
You are calling -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath which is called when you deselect row , instead call
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
It's because you are using didDeSelectRowAtIndexPath: instead of didSelectRowAtIndexPath: method.
You have to call "didSelectRowAtIndexPath" Delegate method.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
This method only return which row we have selected.
I have a simple UITableView
I'm doing my first Edit implementation, to add Delete functionality. Most of this stuff is the default apparently. Pretty much the only method I added to my Controller is
- (void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
Program *victim = Site.current.sortedPrograms[indexPath.row];
[Site.current removeProgram: victim];
[self.tableView
deleteRowsAtIndexPaths: #[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
When I hit the Edit button, I see
which is good. But When I hit the minus button on one with a shorter title, I see the following
The title is completely scrolled out of site! Similarly, if I use the iOS 7 swipe left gesture, even on a longer title, the title is scrolled out of view:
The code I use to create the cell is pretty simple:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ProgramCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Program *program = Site.current.sortedPrograms[indexPath.row];
cell.textLabel.text = program.name;
cell.detailTextLabel.text = [program.script prettyCycleDuration];
return cell;
}
This isn't what I want my users to see. I would rather see it scroll the title only if necessary, so that context (not whitespace) associates with the delete button. I am just using a stock UITableViewCell (not a subclass), and I'm setting the 'style' to Subtitle. I've looked at the delegate methods, and a book I have, but I don't see anything that shows how to deal with this. Is there a way? Will I need to do my own Cell subclass and handle the states more explicitly? Or is there something else I can do?
Update
Even after converting to a custom subclass, with constraint based layouts for my cell, it still slides the whole view over, rather than compress it.
i don no whether this question asked or not but i still searching for an answer.Am working in tableview concept and i am creating a table view like with some set of menu's like inbox, sent,setting etc.Now what i want is i want to create sub menu's inside every menu, for example if i click inbox it should show new,replied,deleted etc like this for every main menu i want to create sub menu. Using array we can load by checking section but with out using array i want to create directly and notable one i have used custom cell i also want to show images as per the menu if it is inbox i have to show inbox image. Could any one help me??
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 8;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 8;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellidentifier=#"ViewProfileCell";
MyHomeViewCell *cell= [[MyHomeViewCell alloc] init];
cell=(MyHomeViewCell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if(!cell)
{
NSArray *nibofMyHomeCell=[[NSBundle mainBundle]loadNibNamed:#"MyHomeViewCell" owner:self options:Nil];
cell=[nibofMyHomeCell objectAtIndex:0];
}
if(indexPath.section==0)
{
cell.MyHomeMenuLabel.text=#"Inbox";
}
}
Here ara a good tuts it could help you:
Expanding/Collapsing TableView Sections
Expandable UITableView
And this github awesome code here by Oliver Letterer.
You can create a UITableview which will contain cells on it as you said inbox, sent, setting etc.
After you need to create another UITableView which will contain your submenu buttons or labels like new,replied, deleted when click on inbox.
Simillarly you will do for rest of the cells in your main UITableView.
And don't get confuse on how will i identify which tableview will get called.
You will check for tableview name like below:
if(tableView==main)
{
///Code for main menu tableview.
}
else if(tableView==sub)
{
////Code for submenu tableview.
}
This you will do in all UITableView Delegate and Datasource methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
There are many ways you can do this. You could for example show each item as a section header and if the section header is tapped you show (or hide) all of the rows in that section. The rows being the 'sub menu' items (new, replied, deleted). Or you could use different sections for each part and how or hide the sections. Or show and hide rows. This is pretty much all controlled by changing the section and row counts.