cellForRowAtIndexPath not get called - ios

I understand that many people have asked about this on stack. However, None of these can solve my problem. I try to build a tableview programmatically and cellForRowAtIndexPath is not called. But when I drag a tableview in storyboard, everything is fine. Here is the related code.
This is the code in viewDidLoad
- (void)viewDidLoad
{
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
self.navigationItem.title = #"发布失物招领启示";
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
[tap setCancelsTouchesInView:NO];
self.itemDetails = #"请输入详情";
self.view.translatesAutoresizingMaskIntoConstraints = NO;
self.tableView = ({
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
tableView.dataSource = self;
tableView.delegate = self;
tableView.translatesAutoresizingMaskIntoConstraints = NO;
[tableView reloadData];
tableView;
});
[self.tableView registerNib:[UINib nibWithNibName:campusCellIdentifier bundle:nil] forCellReuseIdentifier:campusCellIdentifier];
[self.tableView registerNib:[UINib nibWithNibName:imageCellIdentifier bundle:nil] forCellReuseIdentifier:imageCellIdentifier];
[self.tableView registerNib:[UINib nibWithNibName:textFieldCellIdentifier bundle:nil] forCellReuseIdentifier:textFieldCellIdentifier];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
effectView.frame = self.view.bounds;
[self.view addSubview:effectView];
self.postButton = [[AYVibrantButton alloc] initWithFrame:CGRectZero style:AYVibrantButtonStyleFill];
self.postButton.vibrancyEffect = nil;
self.postButton.text = #"发布";
self.postButton.font = [UIFont systemFontOfSize:18.0];
self.postButton.backgroundColor = [UIColor BBTAppGlobalBlue];
self.postButton.translatesAutoresizingMaskIntoConstraints = NO;
[effectView.contentView addSubview:self.postButton];
[self.view addSubview:self.tableView];
//Set up constraints
CGFloat verticalInnerSpacing = 10.0f;
CGFloat buttonHeight = 50.0f;
CGFloat tabBatHeight = self.tabBarController.tabBar.frame.size.height;
CGFloat navigationBarHeight = self.navigationController.navigationBar.frame.size.height;
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_top).offset(navigationBarHeight + 20);
make.bottom.equalTo(self.view.mas_bottom).offset(-tabBatHeight - 2*verticalInnerSpacing - buttonHeight);
make.centerX.equalTo(self.view.mas_centerX);
make.width.equalTo(self.view.mas_width);
}];
[self.postButton mas_makeConstraints:^(MASConstraintMaker *make){
make.bottom.equalTo(self.view.mas_bottom).offset(-tabBatHeight - verticalInnerSpacing);
make.height.equalTo(#(buttonHeight));
make.width.equalTo(self.view.mas_width).multipliedBy(0.55);
make.centerX.equalTo(self.view.mas_centerX);
}];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
And the ones in the protocol.
Thanks for help!

Remove
self.view.translatesAutoresizingMaskIntoConstraints = NO;
From your viewDidLoad, as, in general, the system handles the setting of constraints for your view controller's view. The mechanism by which it does this is the autoresizing mask. When self.view.translatesAutoresizingMaskIntoConstraints = NO the system expects constraints to be provided for it, but no constraints have been added, I'm not sure if there's a defined behavior for this instance, but it's best to avoid it by allowing the system handle the size and position of self.view.

As others have said, if you don't add your table view as a subview of the view controller's content view then it won't try to draw itself and none of the delegate/data source methods will ever be called.
Next point: Where is the "related code" from? What method, and where in that method?
Next point:
What is with this syntax?
self.tableView = ({
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
tableView.dataSource = self;
tableView.delegate = self;
tableView.translatesAutoresizingMaskIntoConstraints = NO;
tableView;
});
Where have you seen that syntax in use in Objective-C code? I have never seen that coding style, and I've been doing Objective-C intensively since about 2007. I gather that the braces denote a scope, and since the last thing in the scope is the expression tableView;, the whole scope evaluates to that variable? Ugh.

You Have To set datasource and delegate of UItableView to your own class.....Other wise your table view will not able to get data from your class variables.....
Follow this link to read about data source and delegation
https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html

Related

how to suspend reloadData of tableview being called?

reloadData is NSLog out after viewDidLayoutSubviews,
I want the tableview call reloadData after GetDataSource , no reloadData on first loading. I even try set _tableView.dataSource = nil; and set it to self in GetDatasource();
But reloadData keeps being called before GetDatasource();
GetDatasource(); is in viewDidload();
viewWillAppar: <ATOMCutstomNavigationController: 0x1476abfc0>
viewDidload
viewWillAppear
viewWillAppear: <ATOMMyFollowViewController: 0x14751b4c0>
viewWillLayoutSubviews
viewDidLayoutSubviews
reloadData
viewWillLayoutSubviews
viewDidLayoutSubviews
viewDidAppear
GetDataSource()
Anyone has any hint?
How can I suspend reloadData or what mistake I have made ?
code In viewDidload:
_myAttentionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAV_HEIGHT - TAB_HEIGHT)];
self.view = _myAttentionView;
_tableView = [[RefreshTableView alloc] initWithFrame:_myAttentionView.bounds];
[_tableView registerClass:[kfcFollowCell class] forCellReuseIdentifier:CellIdentifier];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.estimatedRowHeight = SCREEN_HEIGHT - NAV_HEIGHT - TAB_HEIGHT;
_tableView.delegate = self;
_tableView.psDelegate = self;
_tableView.dataSource = nil;
[_myAttentionView addSubview:_tableView];
_tapMyAttentionGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapMyAttentionGesture:)];
[_tableView addGestureRecognizer:_tapMyAttentionGesture];
_canRefreshFooter = YES;
_dataSource = [NSMutableArray array];
[self firstGetDataSource];
- (void)getDataSource {
Model *sss = [ATOMShowAttention new];
[sss Get:param withBlock:^(NSMutableArray *resultArray, NSError *error) {
if (resultArray.count) {
[_dataSource removeAllObjects];
}
for (ATOMCommonImage *commonImage in resultArray) {
kfcFollowVM * viewModel = [kfcFollowVM new];
[viewModel setViewModelData:commonImage];
[_dataSource addObject:viewModel];
}
if (!_hasSetSource) {
_tableView.dataSource = self;
_hasSetSource = YES;
}
[_tableView reloadData];
[_tableView.header endRefreshing];
}];
}
in view did load
_myAttentionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAV_HEIGHT - TAB_HEIGHT)];
self.view = _myAttentionView;
_tableView = [[RefreshTableView alloc] initWithFrame:_myAttentionView.bounds];
_dataSource = [NSMutableArray array];//do it HERE..........
[self firstGetDataSource];//do it HERE...........
[_tableView registerClass:[kfcFollowCell class] forCellReuseIdentifier:CellIdentifier];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.estimatedRowHeight = SCREEN_HEIGHT - NAV_HEIGHT - TAB_HEIGHT;
_tableView.delegate = self;
_tableView.psDelegate = self;
_tableView.dataSource = nil;
[_myAttentionView addSubview:_tableView];
_tapMyAttentionGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapMyAttentionGesture:)];
[_tableView addGestureRecognizer:_tapMyAttentionGesture];
_canRefreshFooter = YES;

Adding UICollectionViewLayout programmatically issue

I'm trying to implements this library without using storyBoard (first step for implementing this library) , because I'm creating my UIcollectionView programmatically.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.collectionView];
[_collectionView registerNib:[UINib nibWithNibName:#"myCell" bundle:nil] forCellWithReuseIdentifier:#"cell3"];
[_collectionView setBackgroundColor:[UIColor colorWithRed:0.945 green:0.945 blue:0.945 alpha:1] ];
[_collectionView setTransform:CGAffineTransformMakeScale(-1, 1)];
RFQuiltLayout* layout = (id)[_collectionView collectionViewLayout];
layout.direction = UICollectionViewScrollDirectionVertical;
layout.blockPixels = CGSizeMake(100, 100);
}
- (UICollectionView *)collectionView {
if (!_collectionView) {
CGRect collectionViewFrame = self.view.bounds;
collectionViewFrame.size.height -= (self.navigationController.viewControllers.count > 1 ? 0 : (CGRectGetHeight(self.tabBarController.tabBar.bounds))) + 0;
// FMMosaicLayout *mosaicLayout = [[FMMosaicLayout alloc] init];
//// _collectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:mosaicLayout];
// RFQuiltLayout* layout = (id)[_collectionView collectionViewLayout];
// layout.direction = UICollectionViewScrollDirectionVertical;
// layout.blockPixels = CGSizeMake(100, 100);
_collectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:[[RFQuiltLayout alloc] init]];
_collectionView.delegate = self;
_collectionView.dataSource = self;
}
return _collectionView;
}
But this didn't worked and nothing is shown in my view (no error and empty view that's all) Also using debugger I've notified that the UICollectionView Method are never called
First of all, your collectionView method is not called because you are using _collectionView instead self.collectionView in your viewDidLoad method. You need to write self for every property to call their setter and getter methods.
Second, if you want to add custom layout without Storyboard of XIB, then you need to set it programmatically:
RFQuiltLayout* layout = [[RFQuiltLayout alloc] init];
layout.direction = UICollectionViewScrollDirectionVertical;
layout.blockPixels = CGSizeMake(100, 100);
self.collectionView.collectionViewLayout = layout;

Correctly remove object (with code example) iOS

Does the following code (removeViews) correctly remove the reference to the objects, i.e delete them, so I do not keep making more Views when the method createViews is called. createViews creates the views and removeViews sets them to nil. Note: this is a very simple example to enhance understanding, and serves no actual purpose.
-(void) createViews{
UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,200,200)];
tableView.delegate=self;
tableView.datasource = self;
self.mainTableView = tableView;//self.mainTableView is a weak reference
[self.view.superView addSubview: self.mainTableView];
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,400)];
view.backgroundColor = [UIColor redColor];
self.mainView = view;//self.mainView is a strong reference
[self.view.superView addSubviews:self.mainView];
}
-(void) removeViews{
self.mainView = nil;
self.mainTableView=nil;
}
First remove them from their superView and then set them to nil
-(void) removeViews{
[self.mainView removeFromSuperview];
[self.mainTableView removeFromSuperview];
self.mainView = nil;
self.mainTableView=nil;
}

UITableView contentSize wrong when using UISearchBar

UITableView is reporting a bigger contentSize than expected when using a UISearchBar. With zero cells, the expected content height would be zero. Instead, the following code outputs 612 in iPhone 4-inch running iOS 7.
#implementation HPViewController {
UITableView *_tableView;
UISearchBar *_searchBar;
UISearchDisplayController *_searchController;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
_tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_tableView];
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
_searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
_searchController.delegate = self;
_searchController.searchResultsDataSource = self;
_tableView.tableHeaderView = _searchBar;
}
- (void)viewDidLayoutSubviews
{
CGSize contentSize = _tableView.contentSize;
NSLog(#"%f", contentSize.height); // 612.000000
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 0; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return nil; };
#end
Commenting the line that sets the header view makes the code output 0, as expected.
Also, if I assign an empty UIView as the header, the contentSize will be correct and match the height of the header. The problem only happens with UISearchBar.
Is there any way around this? Am I doing something wrong?
Placing the UISearchBar inside a container UIView mostly fixes the problem.
- (void)viewDidLoad
{
[super viewDidLoad];
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
_tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_tableView];
UIView *searchBarContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
_searchBar = [[UISearchBar alloc] initWithFrame:searchBarContainer.bounds];
[searchBarContainer addSubview:_searchBar];
_searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
_searchController.delegate = self;
_searchController.searchResultsDataSource = self;
_tableView.tableHeaderView = searchBarContainer;
}
Unfortunately the UISearchBar glitches in some scenarios that I couldn't isolate yet. I opted to calculate the contentSize manually by adding the height of all cells.
Starting from iOS11, UITableView uses estimate row/header/footer height to calculate initial contentSize by default. In fact, if you tap on the search bar and dismiss the keyboard, content size will have the correct value.
To fix this behavior, set estimate row, header and footer height to 0 in IB instead of Automatic:
or just do it programmatically:
self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;
Add this delegate method
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [[UIView alloc] initWithFrame:CGRectZero];
}
Maybe your view for footer is groing up because there is no cells to show.

iOS 7 UITableView inside a popover

I have a UITableView which has some custom styling. This table view appears in two places in the app, one of which is inside a UIPopoverController. However when the tableview is inside the popover it takes on the default tableview styling as stated in the UI Transition Guide under "Popover".
The problem I have is that there appears to be nowhere to change this behaviour. Regardless of where I try and modify properties of the tableview the view inside the popover doesn't change.
Anyone dealt with this issue before or have any ideas?
Here is the init method of LibraryProductView where I create the table view:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.sectionOrdering = [NSArray arrayWithObjects:
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_DESCRIPTION],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_DOCUMENTS],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_ACTIVE_INGREDIENTS],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_RELATED_PRODUCTS],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_RELATED_DOCUMENTS], nil];
self.backgroundColor = [UIColor whiteColor];
self.tableView = [[UITableView alloc] initWithFrame:CGRectInset(self.bounds, 10, 0) style:UITableViewStyleGrouped];
self.tableView.backgroundColor = [UIColor whiteColor];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.separatorColor = [UIColor clearColor];
self.tableView.showsVerticalScrollIndicator = NO;
[self addSubview:self.tableView];
}
return self;
}
Here is where the containing view (LibraryProductView) is added to the popover:
- (IBAction)didTouchInformationButton:(id)sender
{
if (_infoPopover != nil && _infoPopover.isPopoverVisible)
{
[_infoPopover dismissPopoverAnimated:YES];
return;
}
CGSize preferredSize = CGSizeMake(600.0f, 500.0f);
LibraryProductViewController* productController = [[[LibraryProductViewController alloc] initWithPreferredSize:preferredSize] autorelease];
productController.filterByMyCompany = NO;
productController.product = _activityInput.product;
UINavigationController* nav = [[[UINavigationController alloc] initWithRootViewController:productController] autorelease];
nav.title = _activityInput.product.name;
RELEASE(_infoPopover);
_infoPopover = [[UIPopoverController alloc] initWithContentViewController:nav];
_infoPopover.popoverContentSize = CGSizeMake(preferredSize.width, preferredSize.height + 46);
[_infoPopover presentPopoverFromRect:_infoButton.frame inView:_infoButton permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}
The LibraryProductView is created within viewDidLoad method of LibraryProductViewController.
- (void)viewDidLoad
{
[super viewDidLoad];
self.libraryProductView = [[LibraryProductView alloc] initWithFrame:(usingPreferredSize ? CGRectMake(0.0, 0.0, preferredSize.width, preferredSize.height) : self.view.bounds)];
self.libraryProductView.dataSource = self;
self.libraryProductView.delegate = self;
[self.view addSubview:self.libraryProductView];
}
To set properties for the TableView you might do so in
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
[tableView setBackgroundColor:[UIColor redcolor]];
[tableView setSeparatorColor: [UIColor blueColor]];
return 1;
}
This, of course, assumes you have set UITableViewDataSource in your .h file

Resources