【问题标题】:UITableview automatically reloads inside a UITabbarcontrollerUITableview 在 UITabbar 控制器中自动重新加载
【发布时间】:2015-01-26 10:43:57
【问题描述】:

我有一个uitabbarcontroller,在一个标签内我有一个UIViewcontroller 和一个uitableview。问题是当我切换到另一个选项卡然后再次切换到带有uitableview 的选项卡时,它会自动重新加载。

这是我的代码,我在我的viewWillappear 中调用fetchAppointments:

-(void)fetchAppointments:(NSDate *)startDate andEndDate:(NSDate *)endDate{
    Webservice_2 *web = [[Webservice_2 alloc]init];
    [web fetchAppointmentsWithStart:startDate andEndDate:endDate onCompletion:^(BOOL finished) {
        if(finished){
            [self loadData:_strDate];
            [HUD hide:YES];
        }
    }];

}
-(void)loadData:(NSString *)today{
    NSDate *start = [NSDate dateWithTimeIntervalSince1970:[today floatValue]];

    NSCalendar *calender = [NSCalendar currentCalendar];
    NSCalendarUnit units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *components = [calender components:units fromDate:start];
    components.timeZone = [NSTimeZone localTimeZone];


    NSDateComponents *dateComps = [[NSDateComponents alloc]init];
    [dateComps setDay:[components day]];
    [dateComps setMonth:[components month]];
    [dateComps setYear:[components year]];
    [dateComps setHour:0];
    [dateComps setMinute:0];
    [dateComps setSecond:0];
    NSDate *startDate = [calender dateFromComponents:dateComps];


    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *offset = [[NSDateComponents alloc] init];
    [offset setDay:1];
    NSDate *endDate =  [calendar dateByAddingComponents:offset toDate:startDate options:0];


    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                              @"app_start >= %@ AND app_end <= %@",startDate,endDate];

    appointments = [[Appointment_2 objectsWithPredicate:predicate]sortedResultsUsingProperty:@"app_start" ascending:YES];    NSLog(@"appointments is %@",appointments);

    [tableAppointments reloadData];
}

这是我得到的错误:

*** Terminating app due to uncaught exception 'RLMException', reason: 'Index is out of bounds.'

那是因为在我的CellForRowAtIndexPath 我这样做:

 Appointment_2 *appointment = [appointments objectAtIndex:indexPath.row];

额外信息

  • 我使用Realm 来存储我的数据。 Appointment_2 是RLMObject。
  • 同样在fetchAppointmentsWithStart 中,我删除了所有对象,然后再次添加它们。

我的问题是为什么我的表格视图会自动重新加载??

提前致谢!!

【问题讨论】:

    标签: ios objective-c uitableview uitabbarcontroller realm


    【解决方案1】:

    您自己回答了这个问题:您的UITableView 会自动重新加载,因为您在viewWillAppear 中调用fetchAppointments,这首先会导致数据重新加载(调用loadData:),然后是UITableView 到也重新加载。

    每次您在标签栏中点击一个项目时,相应的视图控制器的viewWillAppear 方法就会被调用。

    希望我的回答很清楚,如果您还有其他问题,请告诉我!

    更新: 顺便说一句,快速解决您的崩溃问题可能是检查 appointments 数组实际上是否包含您尝试从中获取的元素:

    Appointment_2 *appointment = nil;
    if([appointments count] <= indexPath.row){
      appointment = [appointments objectAtIndex:indexPath.row];
    }
    

    【讨论】:

    • 但是只有在我的网络服务调用完成时才调用 loadData ?这样就不会触发重新加载。
    • 是的,在viewWillAppear 中,您首先向网络服务发出请求并重新加载数据!按照代码中的流程进行操作,您会看到...
    • Web 服务调用在fetchAppointments 中实现,您在viewWillAppear 中调用
    • 当我将 NSLog 放在 loadData 中的第一行时。崩溃前没有打印出来?
    • 当我将代码放入 viewDidAppear 而不是 viewWillappear 时,问题由于某种原因得到解决
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多