gpt4 book ai didi

ios - 通过点击 UIButton 重新加载 UIScrollView

转载 作者:行者123 更新时间:2023-11-28 21:53:29 24 4
gpt4 key购买 nike

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

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

我对另一个 NSMUtableArray 做了同样的事情,但这次它从“note”表中读取了所有的 id。它同样在同一个 UIScrollView 中生成新的删除按钮。但是,如果有人点击这些删除按钮,它将从 SQLIte DB 的“note”表中删除该特定注释。 并重新加载 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

最佳答案

这里我们可以感受到UIScrollViewUICollectionView的区别,但是UICollectionView是由UIScrollView组成的, UICollectionView 可以重新加载并相应地调整其内容,而 UIScrollView 则不能。

好的,现在在你的情况下,你必须重新加载(刷新)你的 ScrollView ,这是不可能的,因为我们可以使用 UICollectionViewUITableView

你有两个选择,

最佳选择(有点困难):将 UIScrollView 替换为 UICollectionView - 将花费您一些时间,但对于降低代码复杂性和应用程序的良好性能更好。

糟糕的选择(简单):与 UIScrollView 保持一致 - 当您想要重新加载时,从中删除每个 subview ,然后再次显示并加载所有内容。强烈不推荐。

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

关于ios - 通过点击 UIButton 重新加载 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27393971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com