UITableView over scroll to bottom but top is normal - uitableview

added in viewDidLoad
navgationBar.translucent =N0, iOS 10.3, xcode 8.3
- (void)__setUpUI{
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
[tableView registerNib:[UINib nibWithNibName:#"DXGroupMasterLikesCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:NSStringFromClass([DXGroupMasterLikesCell class])];
_tableView = tableView;
_tableView.backgroundColor = [UIColor orangeColor];
tableView.delegate = self;
tableView.dataSource = self;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:tableView];}
like this
but if I set [self.view addSubview:[UIView new]]; before [self.view addSubview:tableView]; everything is ok
somebody know why ?

Related

How can I finished one view's constrains before other view generate

Now, I am going to generate a titleview that its height is variable for the tableHeaderView.so i need to confirm the titleview's hegiht before the tableview generate
//titleView
JMProductTitleView *titleView = [[JMProductTitleView alloc]initWithFrame:CGRectMake(0, 0, JMDeviceWidth, 300)];
titleView.delegate = self;
JMProductDetailModel *model = [JMPorductDetailTool createProductDetailModel];
titleView.model = model;
_titleView = titleView;
//
UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, JMDeviceWidth, JMDeviceHeight) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
tableView.tableHeaderView = titleView;
[self.view addSubview:tableView];
_tableView = tableView;
You can try using [self.view layoutIfNeeded]. It will force the view to layout all the constraints.

Scroll Doesn't work Help would be graet

I have tried this code but scrollview is not working.
Please help
scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
scrollView.showsVerticalScrollIndicator=YES;
CGRect contentRect = CGRectZero;
for (UIView *view in scrollView.subviews) {
contentRect = CGRectUnion(contentRect, view.frame);
}
scrollView.contentSize = contentRect.size;
//scrollView.contentSize=CGSizeMake(self.view.bounds.size.width,tableView.bounds.size.height+100);
[self.view addSubview:scrollView];
UITableView *tableView=[[UiTableView alloc]initWithFrame:CGRectMake(0,200, self.view.bounds.size.width, self.view.bounds.size.height)];
tableView.dataSource=self;
tableView.delegate=self;
//tableView.keepOneSectionOpen = YES;
//tableView.initialOpenSections = [NSSet setWithObjects:#(0), nil];
tableView.scrollEnabled = NO;
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewCellReuseIdentifier];
[tableView registerNib:[UINib nibWithNibName:#"AccordionHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:kAccordionHeaderViewReuseIdentifier];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width,200)];
imageView.image=[UIImage imageNamed:#"download (1).jpeg"];
imageView.userInteractionEnabled=YES;
imageView.alpha=1;
[scrollView addSubview:imageView];
[scrollView addSubview:tableView];
UITableView is a subclass of UIScrollView, not a subview, so you should just use the table view directly.
For more information read this explanation
I think the way you did is not correct. If you want to add an image above table view, try using tableHeaderView.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
self.tableView.tableHeaderView = imageView;

UITableView Scrolling Horizontal issue

this is my code for creating tableView :
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(8, 0, self.view.frame.size.width - 16, self.view.frame.size.height - 8) style:UITableViewStylePlain];
[_tableView registerNib:[UINib nibWithNibName:#"commentCell" bundle:nil] forCellReuseIdentifier:#"commentCell"];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
the Problem is that my table is scrolling horizontally I need to know how to deactivate it, I've tried bounce = NO; or contentSize but always same issue my table still allow scrolling horizontally
The Issue was caused by [tableView reaload] function in my viewDidAppear , which reload my table before finishing displaying allCell so this caused inappropriate behaviour

UICollectionView with UICollectionFlowLayout not displaying cells in the right location

I have a UICollectionView in a test project that I created which does not use Interface Builder. When I run the app the test views that I give to the collection view, via the datasource, are displayed in the the top right corner around (0,0). And I cannot for the life of me figure out why. I have tried adding constraints to the cell's content view. I have also tried messing with the item insets delegate function, but that does not seem to make a difference. Am I missing something?
Here is the code for the test view controller.
#import "TestViewViewController.h"
#interface TestViewViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
#property (strong, nonatomic) UICollectionView *collectionView;
#property (strong, nonatomic) UICollectionViewFlowLayout *flowLayout;
#property (strong, nonatomic) NSMutableArray *testViews;
#end
#implementation TestViewViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)loadView {
self.view = [[UIView alloc] init];
self.view.backgroundColor = [UIColor whiteColor];
self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
self.testViews = [[NSMutableArray alloc] init];
UIView *testView = [[UIView alloc] init];
testView.backgroundColor = [UIColor blueColor];
testView.translatesAutoresizingMaskIntoConstraints = NO;
UILabel *testLabel = [[UILabel alloc] init];
testLabel.translatesAutoresizingMaskIntoConstraints = NO;
testLabel.text = #"I hate collection views.";
[testView addSubview:testLabel];
testView = [[UIView alloc] init];
testView.backgroundColor = [UIColor redColor];
testView.translatesAutoresizingMaskIntoConstraints = NO;
testLabel = [[UILabel alloc] init];
testLabel.translatesAutoresizingMaskIntoConstraints = NO;
testLabel.text = #"I really do.";
[testView addSubview:testLabel];
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
self.collectionView.translatesAutoresizingMaskIntoConstraints = NO;
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.backgroundColor = [UIColor grayColor];
[self.view addSubview:self.collectionView];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"MyCell"];
NSDictionary *views = #{#"collectionView": self.collectionView};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[collectionView]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[collectionView]|" options:0 metrics:nil views:views]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
#pragma mark - UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return self.testViews.count;
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"MyCell" forIndexPath:indexPath];
NSLog(#"%i", self.testViews.count);
[cell.contentView addSubview: self.testViews[indexPath.row]];
return cell;
}
#pragma mark – UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
// UIView *statView = self.testViews[indexPath.row];
return CGSizeMake(100.0, 100.0);
}
#end
For openers the CGRectZero macro is the equivalent of CGRectMake(0, 0, 0, 0). It would seem that you'd be in trouble with a definition of the space in which the collection view was to be drawn and the CGRectZero origin of 0,0 would put it in the upper left corner of the view. I tried creating an app with your code - and finally got something to show up when I got the code for loadView to look like this:
- (void) loadView
{
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view.backgroundColor = [UIColor whiteColor];
self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
self.testViews = [[NSMutableArray alloc] init];
UIView* testView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 320, 80)];
testView.backgroundColor = [UIColor blueColor];
testView.translatesAutoresizingMaskIntoConstraints = NO;
UILabel* testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
testLabel.translatesAutoresizingMaskIntoConstraints = NO;
testLabel.text = #"I hate collection views.";
[testView addSubview:testLabel];
[self.testViews addObject:testView];
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100, 320, 320) collectionViewLayout:self.flowLayout];
self.collectionView.translatesAutoresizingMaskIntoConstraints = NO;
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.backgroundColor = [UIColor grayColor];
[self.view addSubview:self.collectionView];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"MyCell"];
}
The difference being that objects were created with a frame size - that views were added into views and so on.
My thought is that you'd be better off creating your UICollectionViewin a XIB - setting all the parameters there and also creating a subclass for UICollectionViewCell to be able to do more with the contents of the cells as need be. There's a whole lot more to be done to make this a usable UICollectionView with an arbitrary number of cells, but I have to say, the UICollectionView is not an easy object to deal with - so I sympathize with you as you come up the learning curve.

Trying to construct a tableview with a navigation bar at top

Here's the code I used. What am I missing?
- (void)loadView
{
CGSize screen_size = [[UIScreen mainScreen] bounds].size;
CGFloat navBarHeight = 40;
UINavigationBar *nav = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, screen_size.width, navBarHeight)];
UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, navBarHeight, screen_size.width, screen_size.height - navBarHeight) style:UITableViewStylePlain];
table.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
table.delegate = self;
table.dataSource = self;
table.editing = YES;
[table reloadData];
[self.view addSubview:nav];
[self.view addSubview:table];
[nav release];
[table release];
}
Instead of a nav bar with a table underneath, I get a black screen under the status bar.
You need to create a containing view in your loadView method and set it as the view on your view controller:
- (void)loadView {
CGSize screen_size = [[UIScreen mainScreen] bounds].size;
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0,0,screen_size.width,screen_size.height)];
self.view = myView;
CGFloat navBarHeight = 40;
UINavigationBar *nav = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, screen_size.width, navBarHeight)];
UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, navBarHeight, screen_size.width, screen_size.height - navBarHeight) style:UITableViewStylePlain];
table.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
table.delegate = self;
table.dataSource = self;
table.editing = YES;
[table reloadData];
[self.view addSubview:nav];
[self.view addSubview:table];
[nav release];
[table release];
[myView release];
}
Or alternately, if you have a nib file associated with your view controller then you should be using the viewDidLoad method instead of loadView.

Resources