Tuesday, 14 July 2015

Delete a row in table view IOS

Delete a Row from Table View in IOS



Learn Objective C from here:Click Here

Learn Swift from Here:Click Here

Learn Initializers in Objective C:Click Here


Learn Inheritance in Swift:Click here

Learn IOS Application Development From here:Click Here

Learn Attractive Table View in IOS:Click Here.

Learn Accessory Type in IOS:Click Here

As you have seen in my last tutorials of OtherButton Title manages in  Alert View,Not Read:Click Here.

Deleting a row from table view is very easy just use of one method. In General Table View is represented in Normal Mode.
There is one method behind commitEditingStyle for table view which put table view in Editing mode.

It placed Delete button on right Side automatically.
General Delegate of table view search for EditingStyle method. If it find then when you swipe your row of table view to left it shows you delete button.

Add following code:

-(void)TableView:(UITableView *)TableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
It looks like below:



You can also delete a row from table view by long Pressing On row of table view but you need to add Gestures. But you are not familiar about gestures currently.
I will describe a tutorials about gestures deletion row lator on.


Now delete a row from array with removeObject,removeObjectAtIndex any method.


-(void)TableView:(UITableView *)TableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[studentdata removeObjectAtIndex:indexPath.row];
}

Now row is removed we need to call again Data because it assigned before application is start or during load view in viewdidload method. So table view need to load again.

just write [tableview reloaddata];

below removeObjectAtIndexPath method.

Now the row is deleted from TableView.

It look like:



What happens when you build code again the row comes again. It is due to Loading of Method before the view is loaded the data is loaded into array again. 


Above method of deletion is used on that cases where default values are taken and asked user to remove it according to your need. 

Above method of delete row from table view temporary.


Ok i will give you the solution of it so that Next time you start or build application it will manages array from new data not previous.

Add this code in commitEditingStyle method.
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    lastElements=[studentdata objectAtIndex:indexPath.row];
    k=[studentdata indexOfObject:lastElements];
    NSUserDefaults *alltime=[NSUserDefaults standardUserDefaults];
    [alltime setInteger:k forKey:@"lastvalue"];
    [alltime synchronize];
    [studentdata removeObjectAtIndex:indexPath.row];
    [tableView reloadData];
}
Now add Below code in loadStudentData:
-(void)loadStudentData
{
    
    NSUserDefaults *alltime=[NSUserDefaults standardUserDefaults];
    l=[alltime integerForKey:@"lastvalue"];
    studentdata =[NSMutableArray arrayWithObjects:@"def",@"hjk",@"jko",
                  nil];
 int i;
        for (i=0; i<[studentdata count]; i++) {
            if ([studentdata objectAtIndex:l]==[studentdata objectAtIndex:i]) {
                [studentdata removeObjectAtIndex:l];
            }
        }
}

Now add in Interface the variables we used:
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>

{
    NSMutableArray *studentdata;
     NSString *lastElements;
    NSInteger k;
    NSInteger l;
}

I have use Session but you are not familiar with NSUserDefault Currently. I will Describe it lator.
I have used it save Index of value which delete last time before the next time application start.

Then you would say when i build third time the application why the First row comes again.
The solution to it is i have used a single variable to store previous value you can add array so the value next will not passed.


Source Code Available here:Download here.

I hope you like my tutorials.Any query can mail me or comment here.





My next tutorials will be more options when Swipe the cell.


5 comments:

  1. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in IOS development, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on IOS development . We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us:
    Name : Arunkumar U
    Email : arun@maxmunus.com
    Skype id: training_maxmunus
    Contact No.-+91-9738507310
    Company Website –http://www.maxmunus.com



    ReplyDelete
  2. Great Content !! Thanks for sharing this. One can refer this if someone is looking for iOS app development company to develop iOS application which give high return on their investment.

    ReplyDelete
  3. If you are looking for developing iOS Application Development that could clear the vision for that you need an iOS app development company to catch up to your expectations for the product.

    ReplyDelete