我已经尝试过多次创建表格视图并删除某些行,我什至在这里问过这个问题并实施了他们的建议,但甚至没有得到我的表格
视图控制器.h
@interface XYZViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) UITableView *myTable;
@property (strong, nonatomic) NSMutableArray *names;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *editButton;
- (IBAction)editMyTable:(id)sender;
@end
视图控制器.m
@implementation XYZViewController
@synthesize names, myTable, editButton;
- (void)viewDidLoad
{
[super viewDidLoad];
//input values into the mutable array
names = [[NSMutableArray alloc]initWithObjects:
@"Bob",
@"Chris",
@"Tom"
, nil];
editButton = self.editButtonItem;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"Friends";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.names count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//create an identifier
static NSString *identifier;
//create the cell with the identifier
UITableViewCell *cell = [myTable dequeueReusableCellWithIdentifier:identifier];
//check if cell is nil
if (cell == nil) {
//assign cell
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
//assign the names of the array to each cell
cell.textLabel.text = [names objectAtIndex:indexPath.row];
return cell;
}
- (IBAction)editMyTable:(id)sender
{
[editButton setTitle:@"Done"];
[myTable setEditing:YES animated:YES];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[myTable setEditing:editing animated:animated];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//remove from array
[names removeObjectAtIndex:indexPath.row];
//remove from tableView
[myTable deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
图片: http: //postimg.org/image/g9wqwuo6v/
任何帮助深表感谢!先感谢您!:)