iOS UITableVIew increases cell size on multiple reload - ios

I am developing an app which mostly uses 1 view controller. When the user presses a button it populates an array and uses that array to populate a hidden table view using [myTable reloadData].
Then he can press a button and the table disappears, and with another button it should do the same thing again, populate the same array with the same function and reload data.
But when I do that, the cell's sizes increase and it forces my table subview to increase in size and thus use scrolling to display all the data, while the first time it all fits inside the table subview.
I have probably made an error in the storyboard options of my app, but here's my delegation methods just in case:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"playerCell" forIndexPath:indexPath];
cell.textLabel.text = [NSMutableString stringWithFormat:#"Player %ld", (((Player *)[topPlayersArray objectAtIndex:indexPath.row]).playerID + 1)];
//display the player's poiunts
cell.detailTextLabel.text = [NSMutableString stringWithFormat:#"Points: %ld", (((Player *)[topPlayersArray objectAtIndex:indexPath.row]).playerScore)];
return cell;
}
//custom font and color for the cell
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//set up the cell's font
cell.textLabel.font = [UIFont fontWithName:#"Aka-AcidGR-ScrachThis" size:34.0];
cell.detailTextLabel.font = [UIFont fontWithName:#"Aka-AcidGR-ScrachThis" size:24.0];
cell.detailTextLabel.textColor = [UIColor whiteColor];
//set the color of the label
if(indexPath.row == 0)
{
//set color to gold for first player
cell.textLabel.textColor = [UIColor colorWithRed:252.0/255.0 green:194.0/255.0 blue:0 alpha:1.0];
}
else if(indexPath.row == 1)
{
//set color to silver for second player
cell.textLabel.textColor = [UIColor colorWithRed:191.0/255.0 green:191.0/255.0 blue:191.0 alpha:1.0];
}
else
{
//set color to bronze for 3rd player
cell.textLabel.textColor = [UIColor colorWithRed:0.647 green:0.443 blue:0.392 alpha:1.0];
}
}
//return th e number of rows, should be 0 when the game starts and 2 or 3 depending on the number of players (2 for 2, 3 for 3 or more)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [topPlayersArray count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//create header
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//create the header subview
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 48)];
//create the section title label
UILabel *labelHeader = [[UILabel alloc] initWithFrame:CGRectMake (0,0,320,44)];
//set it to the scrachThis font and white color and center align the text
labelHeader.font = [UIFont fontWithName:#"Aka-AcidGR-ScrachThis" size:34.0];
labelHeader.textColor = [UIColor whiteColor];
labelHeader.textAlignment = NSTextAlignmentCenter;
//set the text and return the headerView
if(thisGame.gameHasEnded)
{
labelHeader.text = #"Winners:";
}
else
{
labelHeader.text = #"Leading Players:";
}
[headerView addSubview: labelHeader];
return headerView;
}
And yes, the table is supposed to have a maximum of 3 cells

Just use:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
And return the proper height.

Related

display list of images in UITableview with trasparent view

Hi I want to display images in UITableview and on top of uitableview I need transparent view.
What I have tried is :
Added view in storyboard and on top of it UITableView.
_vaultView.backgroundColor = [UIColor colorWithRed:26/255.0f green:188/255.0f blue:156/255.0f alpha:0.85f];
Ian unable to display transparent View.
Please suggest me how to proceed further.
edit:
- (void)viewDidLoad {
[super viewDidLoad];
_vaultView.backgroundColor = [UIColor colorWithRed:26/255.0f green:188/255.0f blue:156/255.0f alpha:0.85f];
UILabel *lblMySaved = [self createLabelWithTitle:#"Find all your saved work here" frame:CGRectMake(20,115,300,60) tag:1 font:[Util Font:FontTypeLight Size:18.0] color:[UIColor whiteColor] numberOfLines:0];
[_vaultView addSubview:lblMySaved];
_tblVault.separatorColor = [UIColor clearColor];
_tblVault.delegate = self;
_tblVault.dataSource = self;
_tblVault.scrollEnabled = NO;
[self refreshImages];
}
-(void)refreshImages
{
[aryVaultImages removeAllObjects];
NSArray *aryImages = [self getImagesfromDB]; // Retrieving image paths from DB
[_tblVault reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
return 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
tableView.separatorColor =[UIColor clearColor];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if(indexPath.row ==0 && [aryImgPaths count])
{
NSLog(#"imagepath:%#",[aryImgPaths objectAtIndex:indexPath.row]);
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5,5, 40, 40)];
[imageView setImage:[UIImage imageNamed:#"deletePost11.png"]];
[cell.contentView sendSubviewToBack:imageView];
[cell.contentView addSubview:imageView];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
From my understanding of your Views stack in your viewController, you simply need to move the transparent view to the front of the stack (it is currently at the back of your superView).
You can do this by calling.
[self.view bringSubViewToFront:<your transparent view>];
And in the storyboard just set the view to the correct values
If you want to hide the view just call
[self.view bringSubViewToFront:<your tableView>];
EDIT
To create a transparent view on top of you UITableView, you need to add a UIView in the storyBoard that is a subView of your viewController main view(then set all the attributes you want in the attributes inspector). Your tableView need to be also a subView of your viewController main view.
*make sure that the transparent view is front of the tableView in your storyBoard
In order to handle the gestures (because the transparent view is "blocking" the gestures from the tableView read this post Link)
tableView.backgroundColor = [UIColor clearColor];
tableView.backgroundView.backgroundColor = [UIColor clearColor];
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
if ([view isKindOfClass:[UITableViewHeaderFooterView class]])
{
UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
headerView.contentView.backgroundColor = [UIColor clearColor];
headerView.backgroundView.backgroundColor = [UIColor clearColor];
}
}

How I can Fixed Tableview to Screen

I added Tableview on xib file(you can see on image).tableview is loading well . But last cell is out of screen so when I Swipe Up last index is showing. When I get off my hand, last cell is not appear. I don't change any height of tableview . Why not fixed to my screen ?
I am using reveal menu like facebook in this project : https://github.com/mikefrederick/MFSideMenu
Also You can see problem on movie.
https://www.dropbox.com/s/usfwdhl5w9znkl6/IMG_0006.MOV
viewcontroller.h
#property(nonatomic,retain)IBOutlet UITableView * tableview;
viewcontroller.m
-(void)viewDidAppear:(BOOL)animated
{
self.tableview.backgroundColor=[UIColor colorWithRed:(28/255.0) green:(28/255.0) blue:(28/255.0) alpha:1];
[self.tableview setSeparatorColor:[UIColor blackColor]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
}
#pragma mark -
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==0)
{
return 5;
}
if (section==1)
{
return 3;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text =#"exampleCell";
NSLog(#"cell.contentView.bounds.size.width %1.0f",cell.contentView.bounds.size.width);
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if(section == 1)
return 40;
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
self.tableview.layer.cornerRadius = 10.0f;
tableView.layer.borderWidth = 2.0;
tableView.layer.borderColor = [UIColor redColor].CGColor;
return 60;
}
#pragma mark UITableViewDelegate
- (void)tableView: (UITableView*)tableView
willDisplayCell: (UITableViewCell*)cell
forRowAtIndexPath: (NSIndexPath*)indexPath
{
cell.backgroundColor =[UIColor colorWithRed:(41/255.0) green:(41/255.0) blue:(41/255.0) alpha:1];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
//end of loading
//for example [activityIndicator stopAnimating];
}
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
[headerView setBackgroundColor:[UIColor colorWithRed:(112/255.0) green:(112/255.0) blue:(112/255.0) alpha:1]];
UILabel *titleLabel = [ [UILabel alloc] initWithFrame:CGRectMake(20, 0, 300, 44)];
titleLabel.text = #"MÜŞTERİ ALANI";
titleLabel.textColor = [UIColor whiteColor];
titleLabel.backgroundColor = [UIColor clearColor];
[headerView addSubview:titleLabel];
return headerView;
}
In your video, this menu appears to be a slide out menu, presumably using some third party slide out menu controller library. So this view controller is contained within some container view from that. It is possible that the slide out menu controller isn't properly sizing your view controller to fit its container view.
One solution would be to check any example apps that come with the slide out controller you're using to see if they suffer from the bug and report this to the developer if it is the case. This would be good because other developers would benefit from the improvements. In fact it's possible the slide out controller you're using has fixed this bug with a newer version already that you don't have yet.
Another solution, assuming there's an example app that doesn't suffer from this, is to see how it is adding its slide out menu and see if you're failing to do something that they're doing.
Lastly, if there's no example app or you can't figure out why theirs is working differently, try adding the following to your view controller's viewDidAppear: method:
self.view.frame = self.view.superview.bounds;
This assumes that the container view they create is properly sized.
Your problem seems to be in your xib.
You have to set the size of your View to "Freeform" then add autosizing constraints on your view like you did on your tableView.
first you should select the CELL, not the TABLE VIEW. Next, go to the attribute inspector and click the separator, and you should select the custom inserts. Lastly you can adjust the separator line according to your wish. Hope this will help you and others. Thanks for the question too.

How to Implement DataSheet

I want to implement data sheet to display history of user. I want to implement that design like this:
But I dont know how to do that...Can anyone please help me
Edit:
Add the horizontal line at the specific position and the label at the position and it will look like this.
Create a tableview and than in cellForRowAtIndexPath method add this code..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *SimpleTableIdentifier;
UITableViewCell * cell;
SimpleTableIdentifier = #"SimpleTableIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier: nil];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
UILabel * numLbl = [[UILabel alloc] initWithFrame:CGRectMake(0,5,33,30)];
numLbl.text = #"1";
[numLbl setFont:[UIFont fontWithName:#"Helvetica" size:10.0]];
numLbl.backgroundColor = [UIColor clearColor];
[cell addSubview:numLbl];
UILabel * nameLbl = [[UILabel alloc] initWithFrame:CGRectMake(30,5,50,30)];
nameLbl.text = #"john%Lakeview";
[nameLbl setFont:[UIFont fontWithName:#"Helvetica" size:10.0]];
nameLbl.backgroundColor = [UIColor clearColor];
[cell addSubview:nameLbl];
//create a hoizontal separator in cell to display it like column
UIView* hSeparatorview1 = [[UIView alloc] initWithFrame:CGRectMake(25, 0, 1, 30)];
hSeparatorview1.backgroundColor = [UIColor blackColor];
hSeparatorview1.tag = 1;
[cell addSubview:hSeparatorview1];
UIView* hSeparatorview2 = [[UIView alloc] initWithFrame:CGRectMake(85, 0, 1, 30)];
hSeparatorview2.backgroundColor = [UIColor blackColor];
hSeparatorview2.tag = 2;
[cell addSubview:hSeparatorview2];
}
return cell;
}
//this method is used to set the hight of the tableview cell
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
return 30;
}
I have created it for only two label and two horizontal view but you can create as many as you like.
And yes dot forget to place this code in didSelectRowAtIndexPath otherwise horizontal view will disappear when user click the cell.
- (void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//get the cell which is selected
UITableViewCell *selectedCell = [atableView cellForRowAtIndexPath:indexPath];
//set cell horizontal saparator view color of selected cell bcoz when cell selected all view color is gone
UIView *hSeparatorview1=[selectedCell viewWithTag:1];
hSeparatorview1.backgroundColor = [UIColor blackColor];
UIView *hSeparatorview2=[selectedCell viewWithTag:2];
hSeparatorview2.backgroundColor = [UIColor blackColor];
}
Better to try with Custom tableview Cell for text,and UIViews for lines.I'm also done the same in my app.

UITableViewCell separator refuse to disappear and NSTextAlignmentRight doesn't take effect

I'm using UITableView in my StoryBoard app.
Changing color work, direction doesn't work, separator line still appear.
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"row320x44.png"]];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"row320x44.png"]];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.textLabel.textAlignment = NSTextAlignmentRight;
cell.detailTextLabel.textAlignment = NSTextAlignmentRight;
tableView.separatorColor = [UIColor clearColor];
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"row320x44.png"]];
}
I tried in viewDidLoad:
self.tableViewWallMessages.delegate = self;
self.tableViewWallMessages.separatorColor = [UIColor clearColor];
self.tableViewWallMessages.separatorStyle = UITableViewCellSeparatorStyleNone;
Still nothing.
Any ideas?
Try the following.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//set the separator colour in the table (menu)
tableView.separatorColor = [UIColor clearColor];
// Return the number of sections.
return 1;
}
If that doesnt work then try adding the following in willDisplayCell
cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
Alternatively instead of using storyboard you could create it programatically
in the .h file
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
UITableView *myTableView;
}
#property(nonatomic,strong)UITableView *myTableView;
in the .m file
- (void)viewDidLoad
{
myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
myTableView.dataSource = self;
myTableView.delegate = self;
[myTableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//set the separator colour in the table (menu)
tableView.separatorColor = [UIColor clearColor];
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//set the number of rows in the table (menu) as 6
int noRows = 6;
return noRows;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//set the row height for the table (menu)
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
//Create a title label in the table (menu)
UILabel *titleLabel;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
/**TITLE LABEL SETUP**/
//setup the title label
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 54.0, 77, 33)];
titleLabel.tag = TITLE_TAG;
//add the title label to the cell
[cell.contentView addSubview:titleLabel];
}
else
{
//recall the existing cell content if it has already been created
titleLabel = (UILabel*)[cell.contentView viewWithTag:TITLE_TAG];
}
//set the icon image as the image from the array
//set the menu item title from the array
titleLabel.text = #"some text";
//dont show the clicked cell as highlighted
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Code to execute when a cell is clicked on
}
Try setting Separator value "None" in Storyboard file.
Your code is correct, just make sure you have connected UITableView outlet with storyboard.

How to create UITableView like GRID in landscape mode?

I wanna create the uitableview like this image . loading data from server and assigning the values to columns of Row. I saw the link of stack, but not helpful to me.
UPDATE
My code: -
#pragma mark UITableViewDelegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
return [modelArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
static NSString *AutoCompleteRowIdentifier = #"AutoCompleteRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleGray;
// Configure the cell...
RankModel *model = [modelArray objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%# %# %# %# %# %#", model.level, model.name, model.score, model.rightAnswersCount, model.currentRank, model.country];
return cell;
}
but I wanna display like the given image. so please help me to overcome this problem. thanks in advance.
Well this will require a bit more code than the method you present.
My suggestion is you could create a UILabel for each of your fields, instrad of using a single NSString. Don't use cell.textLabel, but rather add your content on cell.contentView and then you can manage each label's color, background color and labels' sizes. The "grid" look can be rendred by assigning a white color to contentView and assigning a green color for example to each label's background. For example, after your cell is created :
cell.contentView.backgroundColor = [UIColor clearColor];
UILabel* aLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 0.0, 100, 44)];
aLabel.tag = indexPath.row;
aLabel.textAlignment = UITextAlignmentLeft;
aLabel.textColor = [UIColor whiteColor];
aLabel.text = #"Test";
aLabel.backgroundColor = [UIColor greenColor];
[cell.contentView addSubview:aLabel];
[aLabel release];
Start your next label at 201 or more to leave an impression of a white vertical line.
Keep the row index in the tag so you can manage alternate colors in :
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell
*)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0 || indexPath.row%2 == 0) {
// use light green, get access to the labels via [cell.contentView viewWithTag:indexPath.row]
} else {
// use dark green
}
}
Hope this helps.

Resources