Ios 某些重写的UITableView方法在应用程序重新启动后未被调用

Ios 某些重写的UITableView方法在应用程序重新启动后未被调用,ios,objective-c,Ios,Objective C,当我第一次运行程序时,将调用所有重写的UITableView方法。但是,如果我在模拟器中终止应用程序并从模拟器主屏幕重新启动,则不会调用某些重写的方法(例如CommittedItingStyle),而是调用预设方法 有没有一个原因可以解释为什么他们在应用程序重新启动后不会被调用,但在一次正常运行后可以工作 我的表格代码: - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style];

当我第一次运行程序时,将调用所有重写的UITableView方法。但是,如果我在模拟器中终止应用程序并从模拟器主屏幕重新启动,则不会调用某些重写的方法(例如CommittedItingStyle),而是调用预设方法

有没有一个原因可以解释为什么他们在应用程序重新启动后不会被调用,但在一次正常运行后可以工作

我的表格代码:

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // set toolbar delegate for editing toolbar style
    self.aToolbar.delegate = self;

    numberOfSections = 1; // for editing: initial number of sections

    // for testing
    NSLog(@"%@", ptrBookmarks1);

    // load bookmarks segment bar with user's most recent segment selections
    bookmarksSegmentControl.selectedSegmentIndex = selectedBookmarksSegmentIndex;

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.rightBarButtonItem = self.editButtonItem;

    //allow row selection during editing.
    //if the "Add New Song" row is selected we can trigger an insert.
    //rather than forcing the using to click the (+) icon directly
    self.aTableView.allowsSelectionDuringEditing = YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.

    // selected table data
    NSMutableArray *tableData;
    switch (bookmarksSegmentControl.selectedSegmentIndex) {
        case 0: // Bookmarks 1 selected
            tableData = ptrBookmarks1;
            break;
        case 1: // Bookmarks 2 selected
            tableData = ptrBookmarks2;
            break;
        case 2: // My Songs selected
            tableData = ptrMySongs;
            break;
    }

    if ([self isEditing]) // the current view is in editing mode, return count + an extra row
        return [tableData count] + 1;
    else // return count
        return [tableData count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }

    // selected table data
    NSMutableArray *tableData;
    switch (bookmarksSegmentControl.selectedSegmentIndex) {
        case 0: // Bookmarks 1 selected
            tableData = ptrBookmarks1;
            break;
        case 1: // Bookmarks 2 selected
            tableData = ptrBookmarks2;
            break;
        case 2: // My Songs selected
            tableData = ptrMySongs;
            break;
    }

    //if number of rows is greater than the total number of rows in the data set
    //and this view is in editing mode.
    //Initialize the cell for "Add New Song"
    //there will be an extra row once SetEditing: is called
    if(indexPath.row >= tableData.count && [self isEditing]){
        cell.textLabel.text = @"Add New Song";
        //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // display arrow accessory
    }
    else {
        cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
        //cell.accessoryType = UITableViewCellAccessoryNone; // hide arrow accessory
    }
    return cell;

}


//VIEW CONTROLLER METHOD: IMPORTANT
//this is a method of the view controller
//if we use apple's editing button as follows:
//self.navigationItem.rightBarButtonItem = self.editButtonItem
//then this method will be called automatically for us.
//if we are using a button callback or similar method,
//then we need to call setEditing: manually on the view
-(void) setEditing:(BOOL)editing animated:(BOOL)animated{

    //wrap our code in an if statement
    //only run the code if we are not swipe deleting a row.
    //if we were called due to a swipeDelete action, ignore it
    if(isSwipeDeleting == NO){
        //call parent
        [aTableView setEditing:editing];
        [super setEditing:editing animated:animated];


        // selected table data
        NSMutableArray *tableData;
        switch (bookmarksSegmentControl.selectedSegmentIndex) {
            case 0: // Bookmarks 1 selected
                tableData = ptrBookmarks1;
                break;
            case 1: // Bookmarks 2 selected
                tableData = ptrBookmarks2;
                break;
            case 2: // My Songs selected
                tableData = ptrMySongs;
                break;
        }


        //if editing mode
        if(editing){
            //batch the table view changes so that everything happens at once
            [self.aTableView beginUpdates];
            //for each section, insert a row at the end of the table
            for(int i = 0; i < numberOfSections; i++){
                //create an index path for the new row
                NSIndexPath *path = [NSIndexPath indexPathForRow:tableData.count inSection:i];
                //insert the NSIndexPath to create a new row. NOTE: this method takes an array of paths
                [self.aTableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationAutomatic];
            }
            //animate the changes now
            [self.aTableView endUpdates];
        }else{
            //batch the table view changes so that everything happens at once
            [self.aTableView beginUpdates];
            //for each section, insert a row at the end of the table
            for(int i = 0; i < numberOfSections; i++){
                //create an index path for the new row
                NSIndexPath *path = [NSIndexPath indexPathForRow:tableData.count inSection:i];
                //insert the NSIndexPath to create a new row. NOTE: this method takes an array of paths
                [self.aTableView deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationAutomatic];
            }
            //animate the changes now
            [self.aTableView endUpdates];
        }
    }
}


//DELEGATE METHOD:
//this method will be called for every row and allows us to set the
//editing syle icon(Delete,Insert)
-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

    // selected table data
    NSMutableArray *tableData;
    switch (bookmarksSegmentControl.selectedSegmentIndex) {
        case 0: // Bookmarks 1 selected
            tableData = ptrBookmarks1;
            break;
        case 1: // Bookmarks 2 selected
            tableData = ptrBookmarks2;
            break;
        case 2: // My Songs selected
            tableData = ptrMySongs;
            break;
    }

    // Detemine if it's in editing mode
    if (self.editing) {
        //use the + icon(add icon) on row
        //if this is the additional row created in setEditing:animated:
        if(indexPath.row >= tableData.count){
            return UITableViewCellEditingStyleInsert;
        }
        else {
            //use the delete icon on this row
            return UITableViewCellEditingStyleDelete;
        }
    }
    else
        return UITableViewCellEditingStyleNone;
}


//handle the deletion/insertion
//this method is called when the delete or insert icon has been press.
//we should update our dataSource by inserting or removing the selected INDEX
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    // selected table data
    NSMutableArray *tableData;
    switch (bookmarksSegmentControl.selectedSegmentIndex) {
        case 0: // Bookmarks 1 selected
            tableData = ptrBookmarks1;
            break;
        case 1: // Bookmarks 2 selected
            tableData = ptrBookmarks2;
            break;
        case 2: // My Songs selected
            tableData = ptrMySongs;
            break;
    }

    if (editingStyle == UITableViewCellEditingStyleDelete) { // the delete icon was clicked
        // delete pre-test
        NSLog(@"Array before delete: %@", tableData);
        NSLog(@"Directory before delete: ");
        [self readDirectoryContents];

        // if deleting a song or its pages that's in view, switch view to previous MySong if exists, otherwise About page/a blank page
        ////////insert code here////////

        // remove file and its annotations from directory (if deleting from My Songs)
        if (tableData == ptrMySongs) {
            [self deleteImportedSongsheet:tableData[indexPath.row]];

        }

        //remove row from tableData
        [tableData removeObjectAtIndex:indexPath.row];

        //remove the row in the tableView
        [self.aTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];


        // delete post-test
        NSLog(@"Array after delete: %@", tableData);
        NSLog(@"Directory after delete: ");
        [self readDirectoryContents];

        [self.aTableView reloadData];
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) { // the insert icon was clicked
        /*
        //add a new row to the datasource
        [tableData addObject:@"New Icon"];
        //insert a row in the tableView because the plusIcon was clicked.
        [self.aTableView insertRowsAtIndexPaths:@[indexPath]
                              withRowAnimation:UITableViewRowAnimationAutomatic];*/

        self.editing = false; // turn off editing

        // ---SEGUE TO SONG IMPORT OPTIONS TABLE---
        SongImportView *songImportView =
            [[SongImportView alloc]
             initWithNibName:@"SongImportView" bundle:nil];

        // forward the My Songs array
        songImportView.ptrMySongs = ptrMySongs;

        // set delegate
        songImportView.delegate = self;

        // push SongImportView
        [self.navigationController pushViewController:songImportView animated:YES];

        [SongImportView release];
    }
}

//if we are in editing mode we do not want to perform Seque Transition
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
    if ([identifier isEqualToString:@"MyDetailView"] && [self isEditing]) {
        return NO;
    }
    return YES;
}


//this method is called when the user swipes to delete a row.
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath{
    isSwipeDeleting = YES;//user just swipe to delete a row
}
//when the user cancel the swipe or click the delete button
//this method is call
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath{
    isSwipeDeleting = NO;//swipe to delete ended. No longer showing the DELETE button in cell
}

// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return NO;
    //return YES;
}

// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {

    // MUST CHANGE canMoveRowAtIndexPath TO ENABLE THIS

    NSString *item;

    // selected table data
    NSMutableArray *tableData;
    switch (bookmarksSegmentControl.selectedSegmentIndex) {
        case 0: // Bookmarks 1 selected
            tableData = ptrBookmarks1;
            break;
        case 1: // Bookmarks 2 selected
            tableData = ptrBookmarks2;
            break;
        case 2: // My Songs selected
            tableData = ptrMySongs;
            break;
    }

    item = [[tableData objectAtIndex:fromIndexPath.row] retain];
    [tableData removeObjectAtIndex:fromIndexPath.row];
    [tableData insertObject:item atIndex:toIndexPath.row];

    [item release];
}

#pragma mark - Table view delegate

//DELEGATE METHOD:
//the user selected a row
//In order for the user to perform an INSERTION action on a row,
//they have to click the + icon icon. We can implement this method
//so that they can click anywhere on the "Add New Song" to add a new row
//tableView.allowsSelectionDuringEditing = YES; must be set
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    //deselect the selected row with animatiion
    [self.aTableView deselectRowAtIndexPath:indexPath animated:YES];

    // selected table data
    NSMutableArray *tableData;
    switch (bookmarksSegmentControl.selectedSegmentIndex) {
        case 0: // Bookmarks 1 selected
            tableData = ptrBookmarks1;
            break;
        case 1: // Bookmarks 2 selected
            tableData = ptrBookmarks2;
            break;
        case 2: // My Songs selected
            tableData = ptrMySongs;
            break;
    }

    //if the selected  row was the "Add New Song" row call tableView:commitEditingStyle:
    //to add a new row
    if (indexPath.row >= tableData.count && [self isEditing]) {
        [self tableView:tableView commitEditingStyle:UITableViewCellEditingStyleInsert forRowAtIndexPath:indexPath];
    }
    else { // otherwise do regular table item selection
        [self.delegate didTapBookmarksTable:[tableData objectAtIndex:indexPath.row]];
    }

}


// to scroll the table to the top row that was last viewed
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:YES];

    NSIndexPath *iP;

    switch (bookmarksSegmentControl.selectedSegmentIndex) {
        case 0: // Bookmarks 1 selected
            if ([ptrBookmarks1 count] > 0) { // make sure that there is something in the table before scrolling to it, otherwise will crash

                iP = [NSIndexPath indexPathForRow:bookmark1listPositionRow inSection:bookmark1listPositionSection];
                [aTableView scrollToRowAtIndexPath:iP atScrollPosition:UITableViewScrollPositionTop animated:NO];
            }
            break;
        case 1: // Bookmarks 2 selected
            if ([ptrBookmarks2 count] > 0) { // make sure that there is something in the table before scrolling to it, otherwise will crash

                iP = [NSIndexPath indexPathForRow:bookmark2listPositionRow inSection:bookmark2listPositionSection];
                [aTableView scrollToRowAtIndexPath:iP atScrollPosition:UITableViewScrollPositionTop animated:NO];
            }
            break;
        case 2: // My Songs selected
            if ([ptrMySongs count] > 0) { // make sure that there is something in the table before scrolling to it, otherwise will crash

                iP = [NSIndexPath indexPathForRow:mysongslistPositionRow inSection:mysongslistPositionSection];
                [aTableView scrollToRowAtIndexPath:iP atScrollPosition:UITableViewScrollPositionTop animated:NO];
            }
            break;
    }


}

// remember the top row that was last viewed
- (void)viewDidDisappear:(BOOL)animated {

    NSIndexPath *ip;

    switch (bookmarksSegmentControl.selectedSegmentIndex) {
        case 0: // Bookmarks 1 selected
            if ([ptrBookmarks1 count] > 0) { // make sure that there is something in the table before scrolling to it, otherwise will crash
                ip = [[aTableView indexPathsForVisibleRows] objectAtIndex:0];
                bookmark1listPositionSection = ip.section;
                bookmark1listPositionRow = ip.row;
            }
            break;
        case 1: // Bookmarks 2 selected
            if ([ptrBookmarks2 count] > 0) { // make sure that there is something in the table before scrolling to it, otherwise will crash
                ip = [[aTableView indexPathsForVisibleRows] objectAtIndex:0];
                bookmark2listPositionSection = ip.section;
                bookmark2listPositionRow = ip.row;
            }
            break;
        case 2: // My Songs selected
            if ([ptrMySongs count] > 0) { // make sure that there is something in the table before scrolling to it, otherwise will crash
                ip = [[aTableView indexPathsForVisibleRows] objectAtIndex:0];
                mysongslistPositionSection = ip.section;
                mysongslistPositionRow = ip.row;
            }
            break;
    }

}
-(id)initWithStyle:(UITableViewStyle)样式
{
self=[super initWithStyle:style];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
//设置用于编辑工具栏样式的工具栏代理
self.aToolbar.delegate=self;
numberOfSections=1;//用于编辑:节的初始数量
//用于测试
NSLog(@“%@”,ptrBookmarks1);
//加载带有用户最近段选择的书签段栏
bookmarksSegmentControl.selectedSegmentIndex=selectedBookmarksSegmentIndex;
//取消对下一行的注释以保留演示文稿之间的选择。
//self.clearselectiononviewwillappear=否;
//取消对以下行的注释,以在此视图控制器的导航栏中显示编辑按钮。
self.navigationItem.rightBarButtonItem=self.editButtonItem;
//在编辑期间允许行选择。
//如果选择了“添加新歌”行,我们可以触发插入。
//而不是强制用户直接单击(+)图标
self.aTableView.allowselectionduringediting=YES;
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
//返回节数。
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
//返回节中的行数。
//选定表数据
NSMUTABLEARRY*表格数据;
开关(bookmarksSegmentControl.selectedSegmentIndex){
案例0://已选择书签1
tableData=ptrBookmarks1;
打破
案例1://已选择书签2
tableData=ptrBookmarks2;
打破
案例2://我选择的歌曲
tableData=ptrMySongs;
打破
}
如果([self isEditing])//当前视图处于编辑模式,则返回count+一个额外的行
返回[tableData count]+1;
else//返回计数
返回[表数据计数];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“cell”];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:@“cell”];
}
//选定表数据
NSMUTABLEARRY*表格数据;
开关(bookmarksSegmentControl.selectedSegmentIndex){
案例0://已选择书签1
tableData=ptrBookmarks1;
打破
案例1://已选择书签2
tableData=ptrBookmarks2;
打破
案例2://我选择的歌曲
tableData=ptrMySongs;
打破
}
//如果行数大于数据集中的总行数
//此视图处于编辑模式。
//为“添加新歌”初始化单元格
//调用SetEditing:后将有一个额外的行
if(indexath.row>=tableData.count&&[self-isEditing]){
cell.textlab.text=@“添加新歌”;
//cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;//显示箭头附件
}
否则{
cell.textlab.text=[tableData objectAtIndex:indexath.row];
//cell.accessoryType=UITableViewCellAccessoryNone;//隐藏箭头附件
}
返回单元;
}
//视图控制器方法:重要
//这是视图控制器的一种方法
//如果我们使用苹果的编辑按钮,如下所示:
//self.navigationItem.rightBarButtonItem=self.editButtonItem
//然后,我们将自动调用此方法。
//如果我们使用按钮回调或类似方法,
//然后我们需要在视图上调用setEditing:manually
-(void)设置编辑:(BOOL)编辑动画:(BOOL)动画{
//将代码包装在if语句中
//只有在不滑动删除行时才运行代码。
//如果由于swipeDelete操作而调用我们,请忽略它
如果(IssweepDeleting==否){
//打电话给家长
[aTableView setEditing:编辑];
[超级设置编辑:编辑动画:动画];
//选定表数据
NSMUTABLEARRY*表格数据;
开关(bookmarksSegmentControl.selectedSegmentIndex){
案例0://已选择书签1
tableData=ptrBookmarks1;
打破
案例1://已选择书签2
tableData=ptrBookmarks2;
打破
案例2://我选择的歌曲
tableData=ptrMySongs;
打破
}
//如果编辑模式
如果(编辑){
//批处理表视图的更改,以便所有事情都立即发生
[self.aTableView开始更新];
//对于每个部分,在表的末尾插入一行
对于(int i=0;i