I use below code show made table become editable, but seem table view not show delete button on left. Why?
- (void)viewListEditale:(NSNotification *)notification{
NSString *edited = [notification.object objectForKey:#"edit"];
if ([edited isEqualToString:#"N"]) {
[_tableView setEditing:NO animated:YES];
}else{
[_tableView setEditing:YES animated:YES];
}
}
I encountered the same problem with you.last time I forgotten to call:
[super layoutSubviews]
when I call
-(void)layoutSubviews
In my custom Cell.Please check your code.
It may be helpful.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
Related
A UITableViewController is getting loaded with multiselection and ON EDIT mode.
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
....
[self.tableView setEditing:YES animated:YES];
self.tableView.allowsMultipleSelectionDuringEditing = YES;
}
The result is this one:
However this is not what I am looking for, since I want after the viewWillAppear some cells to be already selected.
I would like to be like this
What code do I need on - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath ???
Do I need code in any other method?
You have to keep track of selected ones, that could be BOOL variable in your model, NSArray of indexPaths, or anything in between.
So what you should do in you cellForRowAtIndexPath:
if( dataModel.shouldBeSelected == true){
cell.imageView.image = [UIImage imageNamed:#"selected"];
}
else{
cell.imageView.image = [UIImage imageNamed:#"empty"];
}
note that you have to take care of the non-selected ones so you can prevent reuse of selected cells and showing incorrect results.
How can I deselect a cell when returning to a view?
I have an orange down state which is applied to a cell when selected - the cell navigates to a modal view when clicked - when i click back button the cell is still selected.
I have tried applying this code -
[tableView deselectRowAtIndexPath:indexPath animated:YES];
to my cellForRowAtIndexPath method - but it doesn't do anything!
Update - Having done a bit of research - It appears Ive missed some valuable information out of this question! - my table view is a UITableView embedded in a View Controller - not a UITableViewController - so it sounds like it doesnt have the available methods which are required for the suggestions so far..
You could use UITableViewController's clearsSelectionOnViewWillAppear property.
You should not call deselectRowAtIndexPath in cellForRowAtIndexPath method.
you can do this in viewWillAppear
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear: animated];
NSIndexPath *selectedIndexPath = [tableViewObj indexPathForSelectedRow];
if (selectedIndexPath != nil) {
[tableViewObj deselectRowAtIndexPath:selectedIndexPath animated:YES];
}
}
Or you can write in didSelectRowAtIndexPath as well
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath: indexPath animated:YES];
}
This is the right approach
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath: indexPath animated:NO]; // first line in this method
// rest of code
}
I have a feature in my app where the user can change the color scheme of the app. The app uses a Split View Controller, with a MainTable and DetailView table. Everything works fine except for the MainTable. What is failing is that the MainTable reloadData method is not causing the cells to be redrawn.
It should be noted that I am changing globalHighContrast and sending the notification from a UIModalPresentationFormSheet viewController, so the tables are kind of visible on the screen while the viewController is active.
I am triggering the screen update from a notification, like this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reloadAllTables)
name:#"contrastModeChanged"
object:nil];
Then, to make sure that I call reloadData on the main thread, I am handling the notification like this:
-(void)reloadAllTables{
[self performSelectorOnMainThread:#selector(doReloadAllTables) withObject:nil waitUntilDone:NO];
}
-(void)doReloadAllTables{
[self showIcon];
if( globalHighContrast ){
theTable.backgroundColor = [Colors lightBkgColor];
self.view.backgroundColor = [Colors lightBkgColor];
} else {
theTable.backgroundColor = [Colors darkBkgColor];
self.view.backgroundColor = [Colors darkBkgColor];
}
[detailViewController configureView:currentMainMenu];
[detailViewController.subTable reloadData];
[theTable reloadData];
// desperate try to force it to work
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:currentMainMenu inSection:0];
[self tableView:theTable didSelectRowAtIndexPath:indexPath];
}
Both reloadAllTables and doReloadAllTables are being called, but
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
is not being called.
As soon as I tap a cell on the MainTable it does update correctly to the new color scheme.
Also, there is a desperate attempt to workaround this by trying to simulate the MainTable touch, but that doesn't work either.
You can try to put code for updating you scheme in -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath method...
I have a UITableView which I want to work in a similar way to the Contacts app in that there's an edit button which when clicked transforms the cells into edit cells.
At the moment they are set up using the cell style 'left detail' and I have overridden the setEditing method ready for implementation but I don't know how to transform the cells.
Some other answers on here included "Monitor when the table view's editing property changes (when the Edit button is pressed). Then add code to your delegate methods to compose, draw and indent cells in a different way, when the table view is in editing mode." which is exactly what I want but don't know how to do.
- (void)setEditing:(BOOL)flag animated:(BOOL)animated
{
[super setEditing:flag animated:NO];
if (flag == YES){
// Change views to edit mode.
self.textField = [[UITextField alloc] initWithFrame:[_titleLabel frame]];
[self.textField setText:_titleLabel.text];
[self.view addSubview:self.textField];
}
else {
// Save the changes if needed and change the views to noneditable.
[_titleLabel setText:self.textField.text];
[self.textField removeFromSuperview];
}
}
In my method I have code taken from another question which works.. sort of (it makes a new editable text field on the fly in the wrong place and doesn't hide the label).
The apple guidelines aren't specific enough for me to understand how to develop the views.
In a nutshell, the way this works is you set an edit flag on the entire UITableView and then you implement a couple of methods (canEditRowAtIndexPath,commitEditingStyle) declared in the UITableViewDataSource protocol that determine which cells are being edited.
So first you need to put the UITableVIew into edit mode. You want to do that in the handler for your toolbar button:
[self.tableView setIsEditing:YES animated:NO];
Then, the tableview will call canEditRowAtIndexPath to determine if the row can be edited :
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
Finally, when the user is done editing, this method gets called:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html
There is another example here:
http://www.behindtechlines.com/2012/06/02/enabling-configuring-uitableview-edit-mode/
I have a workaround.
If I create a custom row and make it look the same as the 'left detail' style, but using a textview on the right instead of a label, I can change the 'seteditable' and 'setenabled' fields of the views so that on edit they allow editing. I have hacked the font color so it changes when edit is clicked so the user can see that it is now editable.
This seems very messy - so I'm still looking for the best way to do this.
- (void)setEditing:(BOOL)flag animated:(BOOL)animated
{
[super setEditing:flag animated:NO];
if (flag == YES){
[self.tableView setEditing:YES animated:NO];
[self.sampleLabel setEnabled:YES];
[self.sampleLabel setTextColor:[UIColor blackColor]];
}
else {
[self.sampleLabel setEnabled:NO];
[self.sampleLabel setTextColor:[UIColor darkGrayColor]];
}
}
- (void)configureView
{
self.titleLabel.text = [[self.detailItem valueForKey:#"title"] description];
self.ticketNumberLabel.text = [[self.detailItem valueForKey:#"reference"] description];
self.detailsLabel .text = [[self.detailItem valueForKey:#"details"] description];
self.sampleLabel.text = [[self.detailItem valueForKey:#"reference"] description];
// initially set labels to not be editable
[self.detailsLabel setEditable:NO];
[self.sampleLabel setEnabled:NO];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
// item can't be deleted now
return NO;
}
I have a UITableView with custom UITableViewCells.
The table has two sections, the first section has a single row with a UITextField and can only be edited in terms of the text. This section & row cannot be edited from a UITableView perspective
The second section is a list of cells that are generated from an NSArray. These cells are once again custom UITableViewCells comprising of two UITextFields. These cells can be edited from a UITableView perspective, in the sense that the user can delete and insert rows.
In my designated initializer I have specified self.tableView.editing = YES, also I have implemented the method canEditRowAtIndexPath to return YES.
Problem Statement
The table view does not enter editing mode. I do not see the delete buttons or insert buttons against the rows of section 2. What am I missing?
just a suggestion, check whether your controller fit to these requirements :
i use usual UIViewController and it works fine - you need to :
make your controller a delegate of UITableViewDelegate, UITableViewDataSource
implement - (void)setEditing:(BOOL)editing animated:(BOOL)animated
programmatically add EDIT button - self.navigationItem.rightBarButtonItem = self.editButtonItem (if you add EDIT button from builder you will need to call setEditing : YES manually)
Piece of code :)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:YES];
}
- (void)tableView
:(UITableView *)tableView didSelectRowAtIndexPath
:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
// do not forget interface in header file
#interface ContactsController : ViewController<
UITableViewDelegate,
UITableViewDataSource>
Profit!
What if you do [self tableView setEditing:YES animated:YES]; instead of self.tableView.editing = YES;?