使用 UIButton 从其他 UIViewController 中删除一行
Delete a row from other UIViewController with a UIButton
我有一个问题,我有一个UITableViewController
,里面有一个列表,当我按一行时,它会把我转到另一个UIViewController
。
我用 prepareForSegue
方法实现了这个,我也放了
- (void)tableView:(UITableView *)tableView commitEditingStyle:
当我滑动一行时正在删除。一切都很完美。
现在,我的问题是,我在 UIViewController
中放置了一个 UIButton,我希望在按下它时从 UITableViewController
中删除该行,该怎么做?
commitEditingStyle:
调整您正在查看的内容的核心数据源。删除按钮应该以同样的方式调整核心数据。
看看Apple's Documentation for creating and deleting managed objects。
如果您正在删除该行所代表的数据,并且在您推回 UITableViewController 时仍在查看该行,那么您需要刷新数据。
您可以使用委托,因为您希望在两个视图控制器之间进行通信。
在 DetailViewController 中创建协议。当您最初从 TableViewController 转到 DetailViewController 时,将 "idx" 设置为选定的 indexPath.row 或数组索引。
当您从 DetailViewController 中删除它时,委托会将索引发送到 TableViewController,您可以在那里从主数组中删除它。
DetailViewController.h 文件
#import <UIKit/UIKit.h>
@protocol DetailViewControllerDelegate <NSObject>
@optional
-(void) removeElementAt:(int )index;
@end
@interface DetailViewController : UIViewController{
IBOutlet UIButton *bttn;
id <DetailViewControllerDelegate> delegate;
}
@property (retain) int idx;
@property (retain) id delegate;
-(IBAction)bttnclicked;
-(IBAction)back:(id)sender;
@end
在DetailViewController.m文件中
#import "DetailViewController.h"
#import "TAbleViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
@synthesize idx,delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)bttnclicked{
[[self delegate] removeElementAt:idx];
}
-(IBAction)back:(id)sender{
[self dismissViewControllerAnimated:YES completion:NULL];
}
@end
对于TableViewContoller.h 文件
#import <UIKit/UIKit.h>
#import "DetailViewController.h"
@interface TableViewContoller : UIViewController <DetailViewControllerDelegate>
{
DetailViewController *secondview;
}
@end
在TableViewController.m文件中,
#import "TableViewController.h"
#import "DetailViewController.h"
@interface TableViewController ()
@end
@implementation TableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) removeElementAt:(int )index
{
NSLog(@"Before object : %@",self.objects);
[self.objects removeObjectAtIndex:index];
//reload table at your convinience
NSLog(@"Removed object : %@",self.objects);
[self.tableView reloadData];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DetailViewController *dc=[segue destinationViewController];
dc.idx = (int) indexPath.row;
dc.delegate = self;
}
}
我有一个问题,我有一个UITableViewController
,里面有一个列表,当我按一行时,它会把我转到另一个UIViewController
。
我用 prepareForSegue
方法实现了这个,我也放了
- (void)tableView:(UITableView *)tableView commitEditingStyle:
当我滑动一行时正在删除。一切都很完美。
现在,我的问题是,我在 UIViewController
中放置了一个 UIButton,我希望在按下它时从 UITableViewController
中删除该行,该怎么做?
commitEditingStyle:
调整您正在查看的内容的核心数据源。删除按钮应该以同样的方式调整核心数据。
看看Apple's Documentation for creating and deleting managed objects。
如果您正在删除该行所代表的数据,并且在您推回 UITableViewController 时仍在查看该行,那么您需要刷新数据。
您可以使用委托,因为您希望在两个视图控制器之间进行通信。
在 DetailViewController 中创建协议。当您最初从 TableViewController 转到 DetailViewController 时,将 "idx" 设置为选定的 indexPath.row 或数组索引。
当您从 DetailViewController 中删除它时,委托会将索引发送到 TableViewController,您可以在那里从主数组中删除它。
DetailViewController.h 文件
#import <UIKit/UIKit.h>
@protocol DetailViewControllerDelegate <NSObject>
@optional
-(void) removeElementAt:(int )index;
@end
@interface DetailViewController : UIViewController{
IBOutlet UIButton *bttn;
id <DetailViewControllerDelegate> delegate;
}
@property (retain) int idx;
@property (retain) id delegate;
-(IBAction)bttnclicked;
-(IBAction)back:(id)sender;
@end
在DetailViewController.m文件中
#import "DetailViewController.h"
#import "TAbleViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
@synthesize idx,delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)bttnclicked{
[[self delegate] removeElementAt:idx];
}
-(IBAction)back:(id)sender{
[self dismissViewControllerAnimated:YES completion:NULL];
}
@end
对于TableViewContoller.h 文件
#import <UIKit/UIKit.h>
#import "DetailViewController.h"
@interface TableViewContoller : UIViewController <DetailViewControllerDelegate>
{
DetailViewController *secondview;
}
@end
在TableViewController.m文件中,
#import "TableViewController.h"
#import "DetailViewController.h"
@interface TableViewController ()
@end
@implementation TableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) removeElementAt:(int )index
{
NSLog(@"Before object : %@",self.objects);
[self.objects removeObjectAtIndex:index];
//reload table at your convinience
NSLog(@"Removed object : %@",self.objects);
[self.tableView reloadData];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DetailViewController *dc=[segue destinationViewController];
dc.idx = (int) indexPath.row;
dc.delegate = self;
}
}