按下按钮时UITableView reloadData无法正常工作

时间:2015-06-17 17:36:17

标签: ios objective-c uitableview

我希望self.agendaTablereloadData运行时运行calendarDidDateSelected,但这似乎不会发生。据我所知,表格设置正确,我设置为在重新加载时更新self.agendaTableArraycellForRowAtIndexPath的内容。我做错了什么?

我正在设置表格如下:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Set up Day Agenda table
    CGRect frame = CGRectMake(0,380,self.view.frame.size.width,self.view.frame.size.height);

    UITableView *agendaTable = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
    agendaTable.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;

    agendaTable.delegate = self;
    agendaTable.dataSource = self;
    [agendaTable reloadData];

    [self.view addSubview:agendaTable];
    /////

     self.agendaTableArray = [[NSArray alloc] init];
     self.agendaTableArray = @[@"No events today!"];

    [self.calendar setMenuMonthsView:self.calendarMenuView];
    [self.calendar setContentView:self.calendarContentView];
    [self.calendar setDataSource:self];
}

相关的cellForRowAtIndexPath代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ///...earlier code snipped for brevity

    // title of the item
    if (self.datePicked == [NSNumber numberWithInt:16]) {
        NSLog(@"cellForRowAtIndexPath says self.datePicked is 16");
        self.agendaTableArray = @[@"Dinner with Rebekah", @"Meeting with John"];
    }

    else {
        self.agendaTableArray = @[@"No events today!"];
    }

    cell.textLabel.text = self.agendaTableArray[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    return cell;
}

像这样调用calendarDidDateSelected

- (void)calendarDidDateSelected:(JTCalendar *)calendar date:(NSDate *)date
{
    NSLog(@"Date: %@", date);

    // NSDateFormatter is used to create a date from a string
    // static keyword is used to avoid create a new instance each time calendarDidDateSelected is called
    static NSDateFormatter *dateFormatter = nil;
    if(!dateFormatter){
        dateFormatter = [NSDateFormatter new];
        dateFormatter.dateFormat = @"yyyy-MM-dd"; // Read the documentation for dateFormat
    }

    // If date picked is June 16th
    NSDate *juneSixteenth = [dateFormatter dateFromString:@"2015-06-16"];
    if([juneSixteenth compare:date] == NSOrderedSame){

        self.datePicked = [NSNumber numberWithInt:16];
        NSLog(@"self.datePicked: %@", self.datePicked);

    }
    [self.agendaTable reloadData];
}

1 个答案:

答案 0 :(得分:1)

您是否正确地将自己创建的AgendaTable分配给了' agendaTable' viewDidLoad中的属性?

self.agendaTable = agendaTable;