how to suspend reloadData of tableview being called? - ios

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;

Related

How to use a SearchController.searchBar as a tableview.sectionHeader

question one :
when a SearchController.searchBar as a tableview.tableHeaderView, and tableview have backgroundColor
why it has a strange color: image
question two :
How to use a SearchController.searchBar as a tableview.sectionHeader
when i try do it, my tableView will be so crazy image
code snippet :
- (UISearchController *)searchController{
if (!_searchController) {
_searchController = [[UISearchController alloc] initWithSearchResultsController: self.resultVC];
_searchController.searchResultsUpdater = self;
_searchController.delegate = self;
self.definesPresentationContext = YES;
// 是否添加半透明覆盖
_searchController.dimsBackgroundDuringPresentation = YES;
// 是否隐藏导航栏
_searchController.hidesNavigationBarDuringPresentation = YES;
// 可以通过此种方式修改searchBar的背景颜色
_searchController.searchBar.barTintColor = GL_NAVBAR_COLOR;
UIImageView *barImageView = [[[_searchController.searchBar.subviews firstObject] subviews] firstObject];
barImageView.layer.borderColor = GL_NAVBAR_COLOR.CGColor;
barImageView.layer.borderWidth = 1;
// 可以通过此种方式可以拿到搜索框,修改搜索框的样式
UITextField *searchField = [[[_searchController.searchBar.subviews firstObject] subviews] lastObject];
searchField.backgroundColor = [UIColor yellowColor];
searchField.placeholder = #"请输入搜索内容";
}
return _searchController;
}
- (void)configureUI{
self.tableView = [[UITableView alloc] initWithFrame: self.view.bounds style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview: self.tableView];
self.tableView.backgroundColor = backgroundColor;
self.searchController.searchBar.frame = CGRectMake(0, 0, ScreenWidth, 44);
#if tableHeaderViewSearchBar
self.tableView.tableHeaderView = self.searchController.searchBar;
#endif
}
#if !tableHeaderViewSearchBar
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return self.searchController.searchBar;
}
#endif
You can place the UISearchBar of UISearchController in the navigation bar so that it remains fixed at the top.
So, just add below code at the end of configureUI method and remove last 2 lines of code inside the same method.
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.navigationItem.titleView = self.searchController.searchBar;
self.definesPresentationContext = YES;
I have a provisional idea:
- (void)viewDidLoad {
[super viewDidLoad];
[self.searchController.searchBar sizeToFit];
self.tableView.frame.origin.y = CGRectGetMaxY(self.searchController.searchBar.frame);
[self.view addSubview: self.searchController.searchBar];
}
To answer question one: you need to set tableView.backgroundView
UIView *tableBackgroundView = [[UIView alloc]initWithFrame:self.tableView.bounds];
tableBackgroundView.backgroundColor = GL_BACKGROUD_COLOR;
self.tableView.backgroundView = tableBackgroundView;

cellForRowAtIndexPath not get called

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

Changing Collection view flow layout

I'm having this issue with switching flow layouts, in the view debugger it changes to the flow layout that I want, however in the simulator and on my phone it's a jumbled mess. I've been looking everywhere for an answer or what the problem is and I can't find a solution. ![left side is what im trying to accomplished and how the view debugger looks, right is the simulator][1]
here is the code for the collectionview and the flow layouts -
(void)setupCollectionView
{
CGRect mainscreen = [[UIScreen mainScreen]bounds];
self.originalLayout = [[UICollectionViewFlowLayout alloc]init];
self.otherLayout = [[UICollectionViewFlowLayout alloc]init];
self.collectionView = [[UICollectionView alloc]initWithFrame:mainscreen collectionViewLayout:self.originalLayout];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:#"Cell"];
[self.collectionView setPagingEnabled:YES];
[self.originalLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.originalLayout setMinimumInteritemSpacing:0.0f];
[self.originalLayout setMinimumLineSpacing:0.0f];
self.originalLayout.itemSize = CGSizeMake(self.collectionView.frame.size.width, self.collectionView.frame.size.height);
[self.otherLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.otherLayout setMinimumInteritemSpacing:10.0f];
[self.otherLayout setMinimumLineSpacing:10.0f];
self.otherLayout.itemSize = CGSizeMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
[self.view addSubview:self.collectionView];
}
-(void)changeLayout
{
if (self.topBar.hidden == NO) {
self.topBar.hidden = YES;
self.bottomBar.hidden = YES;
}
if (self.swapCollectionViewLayout == NO) {
self.swapCollectionViewLayout = YES;
[self.collectionView setPagingEnabled:NO];
[self.collectionView setCollectionViewLayout:self.otherLayout animated:YES];
[self.collectionView reloadData];
}
else
{
[self.collectionView setCollectionViewLayout:self.originalLayout animated:YES];
self.collectionView.pagingEnabled = YES;
self.swapCollectionViewLayout = NO;
[self.collectionView reloadData];
}
Here is a link to a photo of the issue
https://www.facebook.com/photo.php?fbid=909831825747700&l=340de9351b

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;

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