What I want to do is:
Setting editing mode on NavigationBar in UITableView adds an edit button on the left side of the UINavigationBar. When I click this button, I'd like an add button to appear on the right side of the NavigationBar.
When I click on the add button, add a row to the NSMutableArray and update the table.
So please give me ideas, code, or links to develop this functionality.
- (IBAction)DeleteButtonAction:(id)sender
{
[arry removeLastObject];
[Table reloadData];
}
- (IBAction) EditTable:(id)sender
{
if(self.editing)
{
[super setEditing:NO animated:NO];
[Table setEditing:NO animated:NO];
[Table reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[Table setEditing:YES animated:YES];
[Table reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Done"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.editing == NO || !indexPath) return UITableViewCellEditingStyleNone;
if (self.editing && indexPath.row == ([arry count]))
{
return UITableViewCellEditingStyleInsert;
} else
{
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[arry removeObjectAtIndex:indexPath.row];
[Table reloadData];
} else if (editingStyle == UITableViewCellEditingStyleInsert)
{
[arry insertObject:#"Tutorial" atIndex:[arry count]];
[Table reloadData];
}
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath
{
NSString *item = [[arry objectAtIndex:fromIndexPath.row] retain];
[arry removeObject:item];
[arry insertObject:item atIndex:toIndexPath.row];
[item release];
}
}
Create a private property to have a reference to the Edit button:
#property (nonatomic) UIBarButtonItem *editButton;
In viewDidLoad:, init the button and assign it to the right button of the navigationItem:
self.editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Edit", nil) style:UIBarButtonItemStylePlain target:self action:#selector(toggleEdit:)];
self.navigationItem.rightBarButtonItem = self.editButton;
Create a method to invoke when the Edit button is tapped. Will change the table's editing mode and update the Edit button title:
- (IBAction)toggleEdit:(id)sender {
[self.myTableView setEditing:!self.myTableView.editing animated: YES];
[self.editButton setTitle:self.myTableView.editing ? NSLocalizedString(#"Done", nil) : NSLocalizedString(#"Edit", nil)];
}
Related
Trying to push to a new view from the results of a search and for some reason the code isn't being called. Sorry I can't be more descriptive.
Here's the code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.table deselectRowAtIndexPath:indexPath animated:YES];
int selectedTemplateID = [((CollectionItem*)[cllct objectAtIndex:indexPath.row]).tID intValue];
ACollection *newView = [[ACollection alloc] initWithID:selectedTemplateID];
newView.template = [cllct objectAtIndex:indexPath.row];
if(tableView == self.table) {
[self.navigationController pushViewController:newView animated:YES];
newView.theTitle = [self.table cellForRowAtIndexPath:indexPath].textLabel.text;
}
if(tableView == self.searchDisplayController.searchResultsTableView) {
NSLog(#"Should push");
[self.navigationController pushViewController:newView animated:YES];
newView.theTitle = [self.table cellForRowAtIndexPath:indexPath].textLabel.text;
}
}
Thanks
First check the datasource and delegat of tableview.
remove [self.table deselectRowAtIndexPath:indexPath animated:YES]; and check once.
I want to save everything in a UITableView controller, with a button, I dont know how to do that. I know it has a saving method, but when you press back to the homescreen of the app, and go back to the viewcontroller, the text is gone, or when i deleted something its still there.
code in my .m file
#import "HomeWorkViewController.h"
#interface HomeWorkViewController ()
#end
#implementation HomeWorkViewController
#synthesize adView;
#synthesize myTableView, numbers;
-(void) viewDidLoad
{
adView.delegate=self;
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
// check here if key exists in the defaults or not, if yes the retrieve results in array
if([[NSUserDefaults standardUserDefaults] objectForKey:#"numberArray"] != nil) {
self.numbers = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"numberArray"]];
}
//Register for the notification when user go to background or minimize the app, just save the array objects in the defaults
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(appWillGoToBackground:)
name:UIApplicationWillResignActiveNotification object:[UIApplication sharedApplication]];
//Add the Add button
UIBarButtonItem * addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target: self action: #selector(insertNewObject)];
self.navigationItem.rightBarButtonItem = addButton;
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.myTableView setEditing:editing animated:animated];
}
-(void)appWillGoToBackground:(NSNotification *)note {
NSLog(#"terminate");
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:self.numbers forKey:#"numberArray"];
[defaults synchronize];
}
-(void)insertNewObject{
//Display a UIAlertView
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter HomeWork" message: #"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//Only perform the following actions if the user hits the ok button
if (buttonIndex == 1)
{
NSString * tmpTextField = [alertView textFieldAtIndex:0].text;
if(!self. numbers){
self.numbers = [[NSMutableArray alloc]init];
}
[self.numbers insertObject:tmpTextField atIndex:0];
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.myTableView insertRowsAtIndexPaths:#[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.numbers.count;
}
-(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];
}
cell.textLabel.text = [self.numbers objectAtIndex:indexPath.row];
return cell;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
//remove our NSMutableArray
[self.numbers removeObjectAtIndex:indexPath.row];
//remove from our tableView
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath
{
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
adView.hidden=FALSE;
NSLog(#"Has ad, showing");
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
adView.hidden=TRUE;
NSLog(#"Has no ads, hiding");
}
-(void)dealloc
{
[adView release];
[super dealloc];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
#end
Looks like you are only saving your data when the application enters the background. If you want to persist the data immediately instead of just when the app closes, just write your array to NSUserDefaults in alertView:clickedButtonAtIndex:
I am setting titles for Tableview using SectionIndexTitlesForTableview method. When I swipe a cell, delete button appears next this titles which looks odd. How to hide this indexTitles when delete button appears and show when delete button disappears.
The inEditMode method hides an index when a table is going to be edited
-(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath{
[self inEditMode:YES];
}
-(void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath{
[self inEditMode:NO];
}
//on self.editButtonItem click
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
[self inEditMode:editing];
}
-(void)inEditMode:(BOOL)inEditMode{
if (inEditMode) { //hide index while in edit mode
self.tableView.sectionIndexMinimumDisplayRowCount = NSIntegerMax;
}else{
self.tableView.sectionIndexMinimumDisplayRowCount = NSIntegerMin;
}
[self.tableView reloadSectionIndexTitles];
}
This might help
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [tableView isEditing] ? nil: #[#"A",#"B",#"C"];
}
- (void)setEditing:(BOOL)editing
{
[super setEditing:editing];
[self reloadSectionIndexTitles];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
_someBoolean = YES;
[tableView reloadData];
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
_someBoolean = NO;
[tableView reloadData];
}
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return _someBoolean ? nil : _yourTitlesArray;
}
Well something is wrong but i dont know what. I cannot delete a row . when my tableview get in editmode red minus signs near all rows appear . touch on them make to appear delete button on row , whem i touch it it become (dark red) and nothing happens.
here is my code (not all) + i have no errors and warnings at compilation and runtime ...
- (void)viewDidLoad {
[super viewDidLoad];
// initialize singleton.
appDelegate = (ExchangedAppDelegate *)[[UIApplication sharedApplication]delegate];
// get banks from settings.plist
NSString *tempPath = [self getPathOfSettingsPlist];
appDelegate.banks = [[NSArray alloc]initWithContentsOfFile:tempPath];
navigationItem.rightBarButtonItem = self.editButtonItem;
backButton = [[UIBarButtonItem alloc]initWithTitle:#"Back" style:UIBarButtonSystemItemCancel target:self action:#selector(closeSettingsView)];
navigationItem.leftBarButtonItem = backButton;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[banksTableView setEditing:editing animated:animated];
if (editing) {
addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addBankFromList)];
navigationItem.leftBarButtonItem = addButton;
} else {
navigationItem.leftBarButtonItem = backButton;
NSString *tempPath = [self getPathOfSettingsPlist];
self.picker.hidden = YES;
[appDelegate.banks writeToFile:tempPath atomically:YES];
}
}
- (void)addBankFromList {
// not interesting
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [appDelegate.banks count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"BanksTableCell";
indexForPath = indexPath;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellEditingStyleNone;
}
cell.textLabel.text = [appDelegate.banks objectAtIndex:indexPath.row];
return cell;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle) editingStyle: forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[appDelegate.banks removeObjectAtIndex:indexPath.row];
[banksTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
[banksTableView reloadData];
}
}
//// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
NSString *temp = [appDelegate.banks objectAtIndex:fromIndexPath.row];
[appDelegate.banks removeObjectAtIndex:fromIndexPath.row];
[appDelegate.banks insertObject:temp atIndex:toIndexPath.row];
}
Actually you have added an extra colon(":") in the commitEditingStyle: method name. Adding the extra colon has made this method a completely different method. So, your view controller thought that you haven't implemented the commitEditingStyle: method and didn't do anything while you edit your table view.
Just remove the colon(":") after editingStyle in the commitEditingStyle: method. That will fix the problem. That line should look like this,
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle) editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
I just add this methods in .h file :
- (IBAction)EditTable:(id)sender;
- (IBAction)DeleteButtonAction:(id)sender;
and in .m file :
(IBAction)DeleteButtonAction:(id)sender{
[tableList removeLastObject];
[Table reloadData];
}
(IBAction) EditTable:(id)sender{
if(self.editing)
{
[super setEditing:NO animated:NO];
[Table setEditing:NO animated:NO];
[Table reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[Table setEditing:YES animated:YES];
[Table reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Done"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
}
}
when I run the program and click the delete button (red button) the program is stop !
whats the problem ? please any help ?
you are evil :(
OK, again my code in .h file is :
- (IBAction)EditTable:(id)sender;
- (IBAction)DeleteButtonAction:(id)sender;
and in .m file is :
- (IBAction)DeleteButtonAction:(id)sender{
[tableList removeLastObject];
[Table reloadData];
}
- (IBAction) EditTable:(id)sender{
if(self.editing)
{
[super setEditing:NO animated:NO];
[Table setEditing:NO animated:NO];
[Table reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[Table setEditing:YES animated:YES];
[Table reloadData];
[self.navigationItem.leftBarButtonItem setTitle:#"Done"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
} }
when I run the program and click the delete button (red button) the program is crash ! whats the problem ? please any help ?
If I am not wrong then you want to delete the cell of the tableView when delete button is clicked....
You need to call one other method of tableview:
//To Delete the Data
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
// Updates the appearance of the Edit|Done button as necessary.
[super setEditing:editing animated:animated];
[tblViewBM setEditing:editing animated:YES];
// Disable the add button while editing.
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//Use your Array from which you need to delete the data.
NSMutableDictionary *dict=(NSMutableDictionary *)[appDel.BookMarkAry objectAtIndex:indexPath.row];
type=[dict valueForKey:#"type"];
[appDel.BookMarkAry removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
I am sure this will Definately help you to delete the cell with data from array also.