UICollectionView display - iphone

Before scrolling
After scrolling
Why is this happening?
Here's the code to add header.
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if (kind == UICollectionElementKindSectionHeader) {
UICollectionReusableView *reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"HeaderView" forIndexPath:indexPath];
if (reusableview==nil) {
reusableview=[[UICollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
}
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
label.text=[NSString stringWithFormat:#"Recipe Group #%i", indexPath.section];
[reusableview addSubview:label];
NSLog(#"%i", indexPath.section);
return reusableview;
}
return nil;
}

Just an hint : it clearly looks like you are overlapping text fields (the first one looks like a "4" overlapped on a "1", the second one like a "3" on a "2").
My guess is you are somewhere in your code a place when you think you are updating a view, whereas you are actually adding a new one to the already existing.
From your (unformatted and out of context) code, it seems you are using [reusableview addSubview:label];. I would look into that bearing my previous remark in mind.

Related

UITableView: custom header title view doesn't show

I want to display a table with custom header titles.
The table view is attached to a controller class that implements the tableview delegate and data source protocols but is not a subclass of UIViewController because the table is a subview to be displayed above another tableview.
some snippets of my code:
The tableview is created programmatically:
_myListView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStyleGrouped];
[_myListView setDataSource:self.myListController];
[_myListView setDelegate:self.myListController];
[_myListView setBackgroundColor:darkBackgroundColor];
where myListController is a strong property in the class.
For the number of rows in sections:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
…
return count;
}
The number of sections:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [someDelegate sectionCount];
}
For the custom Header View:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, SectionHeaderHeight)];
UILabel* sectionHeaderTitle = [[UILabel alloc] initWithFrame:CGRectMake(20, 3, 300, 24)];
[headerView setBackgroundColor:[UIColor clearColor]];
sectionHeaderTitle.text = [self myTitleForHeaderInSection:section];
sectionHeaderTitle.textColor = [UIColor whiteColor];
sectionHeaderTitle.textAlignment = UITextAlignmentLeft;
[headerView addSubview:sectionHeaderTitle];
return headerView;
}
For the custom headerViewHeight (as required since iOS5):
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if ( [self tableView:tableView numberOfRowsInSection:section] > 0) {
return SectionHeaderHeight;
} else {
return 0;
}
}
Sadly, the tableview does not display any section headers just as if I would return nil.
However, I have checked with a breakpoint, that the code actually returns an UIView.
Everything else works fine.
What am I missing? PLease, don't hesitate to make me feel ashamed of my self.
I don't really understand why you want to use a custom view, and not the "standard" one ? You may have your reasons, but I don't see anything in your code telling me why :)
I would personally just use this:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) return #"First section header title";
if (section == 1) return #"Second section header title";
else return nil;
}
Tell me if that's not what you're looking for !
I seem to have found a solution:
I have created a lazy loading strong property for each header view I want to display. (luckily there are only three)
Now the views are shown.
It seems that the header views got deallocated without the strong references before the table was rendered.
Could it be that there is a connection to the class implementing the table view delegate and data source protocols is not a UIViewController?
Text Color you change it to Black color and check once.
sectionHeaderTitle.textColor = [UIColor blackColor];
you try this code this work on my side :-)
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 24)];
UIImage *myImage = [UIImage imageNamed:#"SectionBackGround.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
imageView.frame = CGRectMake(0,0,320,24);
UIImage *imageIcon = [UIImage imageNamed:#"SectionBackGround.png"];
UIImageView *iconView = [[UIImageView alloc] initWithImage:myImage];
iconView.frame = CGRectMake(0,0,320,24);
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 24)];
label.text = [tableView.dataSource tableView:tableView titleForHeaderInSection:section];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:14];
[view addSubview:imageView];
[view addSubview:iconView];
[view addSubview:label];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 24;
}

reducing space between cells in UITableView - why won't this code do this?

any ideas why I can't reduce/remove the space between cells in a Grouped UITableView (one cell per section). I'm including the following in the controller:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0.0;
}
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.0;
}
I'm still getting spaces between cells even with this code. Put a border around the cells and the space isn't from the cells themselves.
As per PengOne's suggestion to look at the answer here, what I didn't have in place that made it work was the following:
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
return [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)] autorelease];
}
-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
return [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)] autorelease];
}

Memory increases drastically when I type in a UITextfield

This is how I have created two textfields in a tableview and i release them in the dealloc method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
cell.accessoryType = UITableViewCellAccessoryNone;
if ([indexPath row] == 0) {
text = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 300, 30)];
text.delegate=self;
text.userInteractionEnabled=NO;
text.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
if ( qNum-1 == 53 ) {
text.placeholder=#"Category";
}
[cell.contentView addSubview:text];
}
if ([indexPath row] == 1) {
text2 = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, 300, 30)];
text2.delegate=self;
text2.userInteractionEnabled=NO;
if(qNum-1==53) {
text2.placeholder=#"Subcategory";
}
text2.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
[cell.contentView addSubview:text2];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
The problem is as soon as the virtual keyboard appears and i start typing into the textfields the memory increases drastically. Can someone please help me out of this problem ? Thanks in advance.
Here is the code I have written inside the delegate methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if([text.text isEqualToString:#"United States"]||[text.text isEqualToString:#"Australia"]){
text2.userInteractionEnabled=NO;
item.title=#"State";
myPickerView.hidden=YES;
myPickerView2.hidden=NO;
[actionSheet showInView:mainView];
[actionSheet setBounds:CGRectMake(0, 0, 320, 400)];
}
}
Your code has several problems. You are adding new UITextFields to your cell, even when reusing it, so you add new text fields over and over to the same cell. You need to build distinct setup logic for the different types of cells within the if (cell == nil) part, and only configure these cells later. Also, you probably want to release the text fields after adding them as a subview. Please refer to the examples in Apple's documentation for UITableView.
You might want to show your delegate methods - maybe there are some more problems.
try this
[cell.contentView addSubview:text];
[text release];
and
[cell.contentView addSubview:text2];
[text2 release];
You are not releasing textfields
Are you using Instruments to detect memory allocation? I've noticed the same exact problem. Memory usage just shoots up when the keyboard shows up... I think it's something you don't have to worry about. There's nothing I did that fixed this, and it pretty much happens in every instance when the keyboard appears. Probably a bug of Instruments?

Having a table view as input view?

I am still struggling with the sign up page for my iphone app - at the moment I am having problems with the gender field.
My idea was that when the user presses the gender field, a table view containing "Male" and "Female" slides up from the bottom and the user then puts a checkmark next to his actual gender. I do not really understand how to correctly obtain this gender table view though.
Currently I have my GenderTableViewCell which points to both a user interaction enabled label and a GenderTableViewController and then I have set the input view of the first to the table view of the latter. In this way a table with "Male" and "Female" actually do slide up when the gender field is touched but as the table is not in grouped style as I specified it to be in GenderTableViewController.xib I have a feeling that I am not on the right track!!
I have been googling and googling but have not been able to find any similar examples - maybe it is not nice to have a table view sliding up? Should I use a UIPicker instead?
I would use a UIPicker instead. It's going to be much easier to implement, and it is a far more standard experience for the user.
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
[tableView setBackgroundColor:[UIColor clearColor]];
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
return 2;
}
-(NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 35;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexpath // set row value for table
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"anycell"];
if(cell == nil)
{
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"anycell"] autorelease];
/// UIImageView *imgView=[[[UIImageView alloc] init] autorelease];
// UIImage *img;
CGRect frame;
frame.origin.x=10;
frame.origin.y=5;
frame.size.width=280;
if(indexpath.section ==0)
{
//img=[UIImage imageNamed:#"textbox1.png"];
frame.size.height=27;
txtname=[[UITextField alloc] initWithFrame:frame];
txtname.delegate=self;
txtname.textColor=[UIColor colorWithRed:211.0/255 green:236.0/255.0 blue:245.0/255.0 alpha:1.0];
txtname.placeholder=#"User Name";
[txtname setAutocorrectionType:UITextAutocorrectionTypeNo];
[txtname setKeyboardType:UIKeyboardTypeURL];
[cell.contentView addSubview:txtname];
}
else if(indexpath.section == 1)
{
frame.size.height=27;
txtpassword=[[UITextField alloc] initWithFrame:frame];
txtpassword.delegate=self;
txtpassword.textColor=[UIColor colorWithRed:211.0/255 green:236.0/255.0 blue:245.0/255.0 alpha:1.0];
txtpassword.secureTextEntry=TRUE;
txtpassword.placeholder=#"Password";
[cell.contentView addSubview:txtpassword];
}
else
{
frame.size.height=27;
//frame.size.width=200;
btnSave=[[UIButton alloc] initWithFrame:frame];
[btnSave setImage:[UIImage imageNamed:#"user-save.png"] forState:UIControlStateNormal];
[btnSave addTarget:self action:#selector(saveClicked) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btnSave];
}
}
if(indexpath.section == 0)
txtname.text=appDelegate.setObj.username;
else if(indexpath.section == 1)
txtpassword.text=appDelegate.setObj.password;
UIImageView *imgView=[[[UIImageView alloc] init] autorelease];
imgView.image=[UIImage imageNamed:#"textbox1.png"];
cell.backgroundView =imgView;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
return cell;
}

Frame of viewForHeaderInSection is always the same size

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(section != 0) {
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 30)] autorelease];
view.backgroundColor = [UIColor redColor];
return view;
} else {
return tableView.tableHeaderView;
}
}
This is my implementation of viewForHeaderInSection but whatever frame I make it's always showing me the same red frame. Do you see any problem with my code?
Image:
UPDATE:
Mhm now my red block is higher but my first tableHeader is now somehow hidden. The first one was implemented with the titleForHeaderInSection. I thought I just implement the height of the tableHeader height but that doesnt work
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if(section == 1)
return 30;
else
return tableView.tableHeaderView.frame.size.height;
}
You need to implement this delegate method
- (CGFloat)tableView:(UITableView *)tableView
heightForHeaderInSection:(NSInteger)section;
In your case, you can simply return 30;.
Also, you are leaking view!
Your [view release] happens after the return. But as soon as the return happens the method execution is aborted and your release is never called.
So you want this instead
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 30)] autorelease];
And get rid of the explicit release down below.