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;
}
Related
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;
}
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 to get data in UITableview instead of tapping on keyboard UITextfield. In UITableview, how to do multiple selection of data.
Edited:- I have a UITextfield. On tapping it, tableview should pop up with data in it instead of keyboard. When a row of tableview is selected, then checkmark should appear and that data should be seen in UITextfield.
Answer of your first question -
Your keyboard will hide by using this code.
-(BOOL)textField:(UITextField *)textField shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if([text isEqualToString:#"\n"]) {
[textField resignFirstResponder];
return NO;
}
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
[self.view endEditing:YES];
// load your tableView here.
// Everything must be custom.that is your table view is just like a popup.
}
Answer of your second question -
Now current scenario is your table view is shown on screen.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *selectedValue = [yourArray objectAtIndex:indexPath.row];
yourTextView.text = selectedValue;
yourTableView.hide = YES;
}
Here after selecting any row that value is shown in yourTextView.but after that you have to hide that tableView. Try this.May this will help you.
-(void)textViewDidBeginEditing:(UITextView *)textView
{
[self.view endEditing:YES]; // this will make key board hide
yourTableView.hide = NO; // here show tableview make sure tableview allocated out side and here you will be just showing.
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
yourTextView.text = yourArray[indexPath.row];
yourTableView.hide = YES;
}
Use UITextField.inputView property.
More on the subject here:
https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/InputViews/InputViews.html
I currently have a table with 8 rows that each have a label on the right side and a button on the left. I was hoping that I could have all the buttons hidden until the user presses an "edit" button in the top right corner and then they would appear allowing the user to interact with each table cell. I don't know if this is possible, because they are in UITableViewCells or if there is an easier method to summoning a button for each cell
UPDATE
okay so I have placed in all the hidden properties and there seem to be no errors, but the app doesn't recognize any of it. The buttons remains unhidden despite the fact that they are set to be initially hidden. Here is my code
Here is my Table Cell code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"BlockCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = #"Free Block";
UIButton*BlockButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
BlockButton.frame = CGRectMake(225.0f, 5.0f, 75.0f, 35.0f);
[BlockButton setTitle:#"Change" forState:UIControlStateNormal];
[BlockButton addTarget:self action:#selector(Switch:) forControlEvents:UIControlEventTouchUpInside];
Blockbutton.backgroundColor = [UIColor colorWithRed:102/255.f
green:0/255.f
blue:51/255.f
alpha:255/255.f];
Blockbutton.hidden = YES;
[cell addSubview:BlockButton];
return cell;
}
and here is my method code:
- (IBAction)Editmode:(UIButton *)sender
{
Blockbutton.hidden = !Blockbutton.hidden;
[self.tableView reloadData];
}
any thoughts or ideas as to what might be the issue?
You'll need to create a UITableViewCell subclass if you don't already have one. In that class, override setEditing:animated: and if the new value is YES, then enable/add/unhide the button.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if (editing) {
// add your button
someButton.hidden = NO;
} else {
// remove your button
someButton.hidden = YES;
}
}
It would be optional, but you are encouraged to animate the change if animated is YES.
Note: this assumes you have the edit button already hooked up the change the editing mode of the UITableView. If you don't, call setEditing:animated: on the UITableView in the button action. This will automatically call setEditing:animated: on each visible table cell.
The trick here is to keep in mind that a table's cells are determined by cellForRowAtIndexPath:. You can cause that method to be called all over again by sending the table reloadData:.
So, just keep a BOOL instance variable / property. Use the button to toggle that instance variable and to call reloadData:. If, at the time cellForRowAtIndexPath: is called, the instance variable is YES, set the button's hidden to YES; if NO, to NO.
take a BOOL variable which defines the whether to show delete button or not, use this BOOL var to for btnName.hidden = boolVar, initially make boolVar = NO, when user taps on edit toggle bool var and reload the tableview.
Another option is to test if you are in edit mode in the cellForRowAtIndexPath method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = //(obtain your cell however you like)
UIButton *button = cell.button; //(get button from cell using a property, a tag, etc.)
BOOL isEditing = self.editing //(obtain the state however you like)
button.hidden = !isEditing;
return cell;
}
And whenever you enter editing mode, reload tableView data. This will make the table view ask for the cells again, but in this case the buttons will be set not to hide.
- (void)enterEditingMode {
self.editing = YES;
[self.tableView reloadData];
}
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;?