iphone - SetNeedsDisplay 不起作用

发布于 2024-11-23 21:17:41 字数 1777 浏览 4 评论 0原文

我正在尝试刷新此代码中的主视图。它显示了正确的子视图,但保留了已显示的其他视图。我已经尝试过 setNeedsDisplay 和 setNeedsLayout 但没有成功。

有什么想法吗?

代码:

-(void)setViewsForLecture:(NSString *)date{
       int scrollHeight = 0;

       [self.view setNeedsDisplay];
       FMDatabase* db = [DatabaseManager openDatabase];
       NSString *query = [NSString stringWithFormat:@"select *  from tbl_lectures where day like \"%%%@%%\" ", date];

       FMResultSet *rs= [db executeQuery:query];

       //Counts how many lectures
       int position = 0;

       while ([rs next]) { 
          NSMutableArray *speakers = [[NSMutableArray alloc] initWithCapacity:30];

           NSString *lectureName = [rs stringForColumn:@"lecturename"];

           FMResultSet *speakersList = [db executeQuery:@SELECT_SPEAKER_FOR_LECTURE, lectureName];

           while ([speakersList next]) {
               [speakers addObject:[speakersList stringForColumn:@"speaker"] ];
           }

           if ([speakers count] % 2 == 0)
           {
               scrollHeight += [speakers count]*40;
           }
           else
           {
               scrollHeight += (1 + ([speakers count] -1)/2)*40;
           }
           NSLog(@"Csantos: speakers for %@: %i",lectureName, [speakers count]);
           UIView *aLecture = [CustomInterface viewForLecture:[rs stringForColumn:@"lecturename"]
                                            onPosition:position
                                          withSpeakers:speakers];



           [self.scroll addSubview:aLecture];

           [aLecture release];
           [speakers release];
           scrollHeight += 280;
           position++;
       }

       self.scroll.contentSize = CGSizeMake(300, scrollHeight);
       [db release];
   }

问候!

I'm trying to refresh my main view in this code. It shows the right sub-view, but it's keeping the other views already shown. I already tried setNeedsDisplay and setNeedsLayout but no success.

Any ideas?

Code:

-(void)setViewsForLecture:(NSString *)date{
       int scrollHeight = 0;

       [self.view setNeedsDisplay];
       FMDatabase* db = [DatabaseManager openDatabase];
       NSString *query = [NSString stringWithFormat:@"select *  from tbl_lectures where day like \"%%%@%%\" ", date];

       FMResultSet *rs= [db executeQuery:query];

       //Counts how many lectures
       int position = 0;

       while ([rs next]) { 
          NSMutableArray *speakers = [[NSMutableArray alloc] initWithCapacity:30];

           NSString *lectureName = [rs stringForColumn:@"lecturename"];

           FMResultSet *speakersList = [db executeQuery:@SELECT_SPEAKER_FOR_LECTURE, lectureName];

           while ([speakersList next]) {
               [speakers addObject:[speakersList stringForColumn:@"speaker"] ];
           }

           if ([speakers count] % 2 == 0)
           {
               scrollHeight += [speakers count]*40;
           }
           else
           {
               scrollHeight += (1 + ([speakers count] -1)/2)*40;
           }
           NSLog(@"Csantos: speakers for %@: %i",lectureName, [speakers count]);
           UIView *aLecture = [CustomInterface viewForLecture:[rs stringForColumn:@"lecturename"]
                                            onPosition:position
                                          withSpeakers:speakers];



           [self.scroll addSubview:aLecture];

           [aLecture release];
           [speakers release];
           scrollHeight += 280;
           position++;
       }

       self.scroll.contentSize = CGSizeMake(300, scrollHeight);
       [db release];
   }

Regards!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海夜未眠 2024-11-30 21:17:41

进行更改后,尝试

  [self.view setNeedsDisplay];

在方法末尾调用。

Try calling

  [self.view setNeedsDisplay];

at the end of the method, after you've made the changes.

如果没有 2024-11-30 21:17:41

我成功了!

我想我不明白 setNeedsDisplay 的工作原理。在这种情况下,它是否也应该重绘我的 ScrollView ?

无论如何,这是现在可以运行的代码:

-(IBAction)clickOnDate1{
    for (UIView *view in [self.scroll subviews]) {
        [view removeFromSuperview];
    }
    NSLog(@"Date1 clicked");
    [self setViewsForLecture:@"4"];
}

感谢 PengOne 的帮助!

I got it working!

I think I didn't understand exactly how setNeedsDisplay works. In this case, was it supposed to redraw my ScrollView too?

Anyway, here is the code that works now:

-(IBAction)clickOnDate1{
    for (UIView *view in [self.scroll subviews]) {
        [view removeFromSuperview];
    }
    NSLog(@"Date1 clicked");
    [self setViewsForLecture:@"4"];
}

and thanks PengOne for trying to help!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文