Search code examples
objective-ciphonexcode7watchkit

How to delete a row from table in watchkit in objective c


I am developing a watchkit application and i have got a table view which must have functionality to delete rows. I saw a tutorial using content menu. But no idea how to delete row from the table. Please help me.

    #import "HomeInterfaceController.h"
#import "HomeTableRowController.h"
#import "DetailHomeInterfaceController.h"

@interface HomeInterfaceController ()
@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceTable *homeTable;
@property (nonatomic,strong) NSArray *nameArr;

@end

@implementation HomeInterfaceController

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];
    self.nameArr = [NSArray arrayWithObjects: @"Jill Valentine", @"Peter Griffin", @"Meg Griffin", @"Jack Lolwut",
                    @"Mike Roflcoptor", @"Cindy Woods", @"Jessica Windmill", @"Alexander The Great",
                    @"Sarah Peterson", @"Scott Scottland", @"Geoff Fanta", @"Amanda Pope", @"Michael Meyers",
                    @"Richard Biggus", @"Montey Python", @"Mike Wut", @"Fake Person", @"Chair",
                    nil];
    [self setupTable];

    // Configure interface objects here.
}

- (void)willActivate {
    // This method is called when watch view controller is about to be visible to user
    [super willActivate];
}

- (void)didDeactivate {
    // This method is called when watch view controller is no longer visible
    [super didDeactivate];
}

- (void)setupTable {

    [self.homeTable setNumberOfRows:[self.nameArr count] withRowType:@"HomeTableRowController"];

    for (NSInteger i = 0; i < self.nameArr.count; i++)
    {

        HomeTableRowController *row = [self.homeTable rowControllerAtIndex:i];
        NSString *thisBook = [self.nameArr objectAtIndex:i];

        [row.reminderHeadlineLabel setText:thisBook];
       // [row.imageRow setImage:[UIImage imageNamed:@"abc.jpg"]];

    }


}

- (void)table:(WKInterfaceTable *)table didSelectRowAtIndex:(NSInteger)rowIndex
{
   NSDictionary *d=[NSDictionary dictionaryWithObject:@"hi" forKey:@"nm"];
    [self presentControllerWithName:@"DetailHome" context:d];
}
- (IBAction)deleteButtonAction {
}

@end

Solution

  • You need to call removeRowsAtIndexes in your deleteButtonAction function:

    // first create an index set with the index of the row you want to delete
    NSIndexSet *indexes = [[NSIndexSet alloc] initWithIndex:index];
    // then call the function
    [self.homeTable removeRowsAtIndexes:indexes];
    

    There is more info in the docs