通过点击UIButton重新加载UIScrollView

时间:2014-12-10 05:36:02

标签: ios objective-c uiscrollview uibutton

让我先解释一下我的项目。我在我的SQLIte数据库表中有一些名为" note"的数据。 在"注意"我有以下字段:idnoteTokennote

我在这里做的是从该表中加载note中的所有NSMUtableArray。然后根据UIButton内容编号创建array,并将这些按钮添加到UIScrollView subViewscrollview的按钮数和宽度根据该数组的内容数生成auto。现在,当有人点击其中一个按钮时,它会将他带到下一个viewController,并在viewController显示相应的注释详细信息。

我对另一个NSMUtableArray执行相同操作,但这次它会从"注释"中读取所有id。表。它同样在同一UIScrollView中生成新的删除按钮。但是,如果有人点击这些删除按钮,它将删除表中的特定注释"注意" SQLIte DB的。{ AND RELOAD THE UIScrollView 。除 RELOAD THE UIScrollView部分外,所有操作均已完成。这就是我要的。我尝试了所有存在的解决方案,但不知道它为什么不起作用 这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.noteToken = [NSString stringWithFormat:@"%@%@", fairId, exibitorId];

    scrollViewNoteWidth = 100;
    [scrollViewNote setScrollEnabled:YES];
    [scrollViewNote setContentSize:CGSizeMake((noteButtonWidth * countNoteButtonArray) + scrollViewNoteWidth, 100)];

    sqLite = [[SQLite alloc] init];
    [self.sqLite callDataBaseAndNoteTableMethods];

    self.noteButtonArrayy = [[NSMutableArray alloc] init];
    noteButtonArrayy = [self.sqLite returnDataFromNoteTable:noteToken];

    [self LoadNoteButtonAndDeleteButton:noteButtonArrayy];
}


//////////////*----------------------- Note Section (Down) -----------------------*//////////////
-(void) LoadNoteButtonAndDeleteButton:(NSMutableArray *) noteButtonArray
{
    sQLiteClass = [[SQLiteClass alloc] init];
    noteButtonArrayToShowNoteButton = [[NSMutableArray alloc] init];

    /*--------------- Load the noteButton & pass note (Down)---------------*/
    for (int i = 0; i < [noteButtonArray count]; i++)
    {
        sQLiteClass = [noteButtonArray objectAtIndex:i];
        // NSString *ids = [NSString stringWithFormat:@"%d", sQLiteClass.idNum];
        NSString *nt = sQLiteClass.note;
        [noteButtonArrayToShowNoteButton addObject:nt];
    }
    [self ShowNoteButtonMethod:noteButtonArrayToShowNoteButton];
    /*--------------- Load the noteButton & pass note (Up)---------------*/

    /*--------------- Load the deleteButton & pass id (Down)---------------*/
    noteButtonArrayToDeleteNoteButton = [[NSMutableArray alloc] init];
    for (int i = 0; i < [noteButtonArray count]; i++)
    {
        sQLiteClass = [noteButtonArray objectAtIndex:i];
        // Convert int into NSString
        NSString *ids = [NSString stringWithFormat:@"%d", sQLiteClass.idNum];
        [noteButtonArrayToDeleteNoteButton addObject:ids];
    }
    [self ShowNoteDeleteButtonMethod:noteButtonArrayToDeleteNoteButton];
    /*--------------- Load the deleteButton & pass id (Down)---------------*/
}

-(void) ShowNoteButtonMethod:(NSMutableArray *) btnarray
{
    countNoteButtonArray = [btnarray count];

    // For note button
    noteButtonWidth = 60;
    noteButtonXposition = 8;
    for (NSString *urls in btnarray)
    {
        noteButtonXposition = [self addNoteButton:noteButtonXposition AndURL:urls];
    }
}

-(int) addNoteButton:(int) xposition AndURL:(NSString *) urls
{
    noteButton =[ButtonClass buttonWithType:UIButtonTypeCustom];
    noteButton.frame = CGRectMake(noteButtonXposition, 8.0, noteButtonWidth, 60.0);
    [noteButton setImage:[UIImage imageNamed:@"note.png"] forState:UIControlStateNormal];
    [noteButton addTarget:self action:@selector(tapOnNoteButton:) forControlEvents:UIControlEventTouchUpInside];
    [noteButton setUrl:urls];
    noteButton.backgroundColor = [UIColor clearColor];
    [self.scrollViewNote addSubview:noteButton];
    noteButtonXposition = noteButtonXposition + noteButtonWidth + 18;

    return noteButtonXposition;
}

-(void)tapOnNoteButton:(ButtonClass*)sender
{
    urlNote = sender.url;
    [self performSegueWithIdentifier:@"goToNoteDetailsViewController" sender:urlNote];
}

-(void) ShowNoteDeleteButtonMethod:(NSMutableArray *) btnarray
{
    countNoteButtonArray = [btnarray count];

    // For delete button
    deleteNoteButtonWidth = 14;
    deleteNoteButtonXposition = 31;
    for (NSString *idNumber in btnarray)
    {
        deleteNoteButtonXposition = [self addDeleteButton:deleteNoteButtonXposition AndURL:idNumber];
    }
}

-(int) addDeleteButton:(int) xposition AndURL:(NSString *) idNumber
{
    deleteNoteButton =[ButtonClass buttonWithType:UIButtonTypeCustom];
    deleteNoteButton.frame = CGRectMake(deleteNoteButtonXposition, 74.0, deleteNoteButtonWidth, 20.0);
    [deleteNoteButton setImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
    [deleteNoteButton addTarget:self action:@selector(tapOnDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
    [deleteNoteButton setIdNum:idNumber];
    deleteNoteButton.backgroundColor = [UIColor clearColor];
    [self.scrollViewNote addSubview:deleteNoteButton];
    deleteNoteButtonXposition = deleteNoteButtonXposition + deleteNoteButtonWidth + 65;

    return deleteNoteButtonXposition;
}

-(void)tapOnDeleteButton:(ButtonClass*)sender
{
    idNumb = sender.idNum;
    [self.sqLite deleteData:[NSString stringWithFormat:@"DELETE FROM note WHERE id IS '%@'", idNumb]];
    // NSLog(@"idNumb %@", idNumb);

    //[self.view setNeedsDisplay];
    //[self.view setNeedsLayout];
    //[self LoadNoteButtonAndDeleteButton];
    //[self viewDidLoad];

//    if ([self isViewLoaded])
//    {
//        //self.view = Nil;
//        //[self viewDidLoad];
//        [self LoadNoteButtonAndDeleteButton];
//    }
}
//////////////*----------------------- Note Section (Up) -----------------------*//////////////

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"goToNoteDetailsViewController"])
    {
        NoteDetailsViewController *noteDetailsViewController = [segue destinationViewController];
        [noteDetailsViewController setUrl:sender];
    }
}

这是屏幕截图:

enter image description here

1 个答案:

答案 0 :(得分:2)

我们可以感受到UIScrollViewUICollectionView之间的区别,但UICollectionViewUIScrollView组成,UICollectionView可以重新加载并调整其内容因此,UIScrollView可以在哪里。

好的,现在在您的情况下,您已经重新加载(刷新)滚动视图,这是UICollectionViewUITableView无法实现的。

您有两个选择,

最佳选择(稍微强硬):将UIScrollView替换为UICollectionView - 将花费您的一些时间,但更好地降低代码复杂性和应用程序的良好性能。

选项不佳(简单):与UIScrollView保持一致 - 当您想要重新加载时,从中删除每个子视图,然后再次显示并加载所有内容。非常不推荐。

恕我直言,你应该选择最好的选择。