В моем приложении iOS у меня есть панель вкладок с двумя вкладками, как показано ниже. Для каждой вкладки я создал объект UITableView программно. Как добавить UITableViewCell из раскадровки в программно созданный объект UITableView?
Я создал панель вкладок, используя коллекцию. Затем я добавил UITableViewCell к контроллеру просмотра из раскадровки.
Так что я получил следующий вид.
Но теперь, когда я запускаю мое приложение, я не могу видеть UITableViewCell. Где я делаю ошибку, я не понимаю. Пожалуйста помоги.
Ниже приведен мой код.
#import "TicketsViewController.h"
#import "TabBarCollectionViewCell.h"
#import "TicketTableViewCell.h"
#import "Constants.h"
@interface TicketsViewController() <UICollectionViewDelegate, UICollectionViewDataSource, UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *tabBarCollectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *tabBarCollViewFlowLayout;
@property (weak, nonatomic) NSIndexPath *prevSelecedIndexPath;
@property (strong, nonatomic) NSArray *tabBarItemNamesArray;
@property (weak, nonatomic) IBOutlet UIView *parentViewForTableViews;
@property (weak, nonatomic) UIView *previousView;
@property (strong, nonatomic) NSMutableArray *tabRespectiveTableViewsArray;
@end
@implementation TicketsViewController
#pragma mark - lazy instantiation
- (NSArray *)tabBarItemNamesArray {
if (!_tabBarItemNamesArray)
_tabBarItemNamesArray = @[@"All Tickets", @"Resolved Tickets"];
return _tabBarItemNamesArray;
}
- (NSMutableArray *)tabRespectiveTableViewsArray {
if (!_tabRespectiveTableViewsArray)
_tabRespectiveTableViewsArray = [[NSMutableArray alloc] initWithArray:@[(id)[NSNull null], (id)[NSNull null]]];
return _tabRespectiveTableViewsArray;
}
#pragma mark - life cycle
- (void)viewDidLoad {
[super viewDidLoad];
[self.view layoutIfNeeded];
[self tabBarItemNamesArray];
[self tabRespectiveTableViewsArray];
_tabBarCollectionView.delegate = self;
_tabBarCollectionView.dataSource = self;
_previousView = nil;
_tabBarCollViewFlowLayout.itemSize = CGSizeMake(ceilf(_tabBarCollectionView.frame.size.width/2), _tabBarCollViewFlowLayout.itemSize.height);
_prevSelecedIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
[_tabBarCollectionView selectItemAtIndexPath:_prevSelecedIndexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
#pragma mark - collection view
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return _tabBarItemNamesArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
TabBarCollectionViewCell *tabBarCollectionViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TabBarCollectionViewCell" forIndexPath:indexPath];
tabBarCollectionViewCell.tabView.tabTitle = [_tabBarItemNamesArray objectAtIndex:indexPath.row];
if ([_tabRespectiveTableViewsArray objectAtIndex:indexPath.row] == (id)[NSNull null]) {
UITableView *tableView = [[UITableView alloc] initWithFrame:_parentViewForTableViews.bounds style:UITableViewStylePlain];
[tableView registerClass:[TicketTableViewCell class] forCellReuseIdentifier:@"TicketTableViewCell"];
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
tableView.showsVerticalScrollIndicator = YES;
tableView.showsHorizontalScrollIndicator = NO;
tableView.scrollEnabled = YES;
tableView.pagingEnabled = NO;
tableView.bounces = YES;
tableView.userInteractionEnabled = YES;
tableView.backgroundColor = [UIColor clearColor];
tableView.translatesAutoresizingMaskIntoConstraints = NO;
[_parentViewForTableViews addSubview:tableView];
[_tabRespectiveTableViewsArray replaceObjectAtIndex:indexPath.row withObject:tableView];
[_parentViewForTableViews addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[v0]|" options:0 metrics:nil views:@{@"v0" : tableView}]];
if (_previousView == nil) {
[_parentViewForTableViews addConstraint:[NSLayoutConstraint constraintWithItem:tableView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:_parentViewForTableViews attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f]];
_previousView = tableView;
}
else {
[_parentViewForTableViews addConstraint:[NSLayoutConstraint constraintWithItem:tableView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:_previousView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f]];
}
[_parentViewForTableViews addConstraint:[NSLayoutConstraint constraintWithItem:tableView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:_parentViewForTableViews attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0.0f]];
tableView.dataSource = self;
tableView.delegate = self;
}
return tabBarCollectionViewCell;
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
((TabBarCollectionViewCell *)cell).tabView.tabSelected = cell.isSelected;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (_prevSelecedIndexPath)
((TabBarCollectionViewCell *)[collectionView cellForItemAtIndexPath:_prevSelecedIndexPath]).tabView.tabSelected = NO;
((TabBarCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]).tabView.tabSelected = YES;
_prevSelecedIndexPath = indexPath;
[collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
[((UITableView *)[_tabRespectiveTableViewsArray objectAtIndex:indexPath.row]) reloadData];
}
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
((TabBarCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]).tabView.tabHighlighted = YES;
}
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
((TabBarCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]).tabView.tabHighlighted = NO;
}
#pragma mark - table view
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TicketTableViewCell *ticketTableViewCell = [tableView dequeueReusableCellWithIdentifier:@"TicketTableViewCell" forIndexPath:indexPath];
ticketTableViewCell.labelTime.text = @"02:45 PM";
return ticketTableViewCell;
}
#pragma mark - button actions
- (IBAction)buttonOpenDrawerAction:(id)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:SHOW_LEFT_DRAWER object:self];
}
@end
Вы должны написать TableView delgate и DataSource методы collectionViewcell. – Poles
@ Поля - пожалуйста, проверьте код. Я уже сделал это – appleBoy21
Не получив на самом деле, вопрос не совсем ясен, PLS укажет, где у вас есть табличное представление и на какой части у вас есть viewview. И попытайтесь опубликовать полный образ пользовательского интерфейса. –