Подтвердить что ты не робот

Как использовать TableView внутри viewcontroller?

В раскадровке я добавил табличное представление к моему контроллеру представления, у меня ctrl перетащил TableView на Viewcontroller и подключил "делегат" и "источник данных". В файле (.h) я добавил <UITableViewDataSource,UITableViewDelegate>, но когда я запустил приложение, я просто получил ошибку SIGABRT (?), И приложение завершилось сбой. Что мне делать?

4b9b3361

Ответ 1

До сих пор так хорошо, вам просто нужно реализовать UITableViewDataSource и UITableViewDelegate в вашем файле реализации.

Необходимые функции:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [regions count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Number of rows is the number of time zones in the region for the specified section.
    Region *region = [regions objectAtIndex:section];
    return [region.timeZoneWrappers count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    // The header for the section is the region name -- get this from the region at the section index.
    Region *region = [regions objectAtIndex:section];
    return [region name];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"MyReuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:MyIdentifier]];
    }
    Region *region = [regions objectAtIndex:indexPath.section];
    TimeZoneWrapper *timeZoneWrapper = [region.timeZoneWrappers objectAtIndex:indexPath.row];
    cell.textLabel.text = timeZoneWrapper.localeName;
    return cell;
}

Здесь ссылка для документации Apple