Can a UITableViewCell know that it should be in grouped mode? - iphone

Is there a way for a cell, if used in a grouped table view, detect that fact and modify its appearance as ro not overlap the rounded rect borders?

There are two ways to do this. If you are making a custom cell you can just add a boolean property to the cell and set it when you create the cell. Or, you can do this:
UITableView *parentTable = (UITableView *)self.superview;
if (parentTable.style == UITableViewstyleGrouped {
//Do what you need to do for a grouped cell
}
else {
//Do what you need to do for a plain cell
}

In a grouped table view, such a cell will always be the 1st or last of the section. You can easily identify it in cellForRowAtIndexPath:, by looking at the indexPath parameter.

Related

dynamic uitableviewcell height according to custom cell height

I am wondering, is there a way to read the height of the custom tableview cell you are going to use in your uitableviewcell which you can then say to your uitableviewcell be this ---> height?
I am not fully understand your question but do you want to set the height for each custom cell ? If yes then you can do it by :
Let say I have custom cell at index 0 rest are my default cells or can be custom cell as well .
- (CGFloat)tableView:(UITableView *)_tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0 ) {
return 105.0;
}else {
return 80.0;
}
}
No, the height that you set in the xib/storyboard is for display purposes only and doesn't transfer over to code.
The only way to change the height is from your custom subclass, by overriding tableView:heightForRowAtIndexPath:.
If you have a lot of different heights, you could always subclass UITableViewCell and add a height property which tableView:heightForRowAtIndexPath: uses when it sees your subclass. Then, in your storyboard, select your cell, go to the identity inspector, and add a User Defined Runtime Attribute for height with the value that you need.

How to limit the number of cells UITableview displays?

Im using a tableview to display some information in a quiz app that Im working on. My question is how do i make the tableview only show the number of cells that I need. Ive set the number of rows delegate method like this:
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
but at the bottom of the table view are empty cells that are not needed. If I set the tableview style to grouped I get 5 cells and no empty ones below them. Ive seen that other people have done this but cant seem to work it out. I was wondering if they have somehow added a custom view to the table footer to cancel the empty cells out?
Any ideas or help appreciated.
If you do want to keep the separator, you could insert a dummy footer view. This will limit the tableview to only show the amount of cells you returned in tableView:numberOfRowsInSection:
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
In swift:
self.tableView.tableFooterView = UIView(frame: CGRectZero)
A much nicer method which doesn't require cell resizing is to turn off the default separator (set the style to none) and then have a separator line in the cell itself.
I was having a similar problem, how to show only separators for the cells that contain data.
What I did was the following:
Disable separators for the whole tableView. You can do that in the
inspector for the tableview in Interface builder or by calling
[yourTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];.
Inside your cellForRowAtIndexPath where you populate your tableview with cells create a new UIView and set it as a subview to the cell. Have the background of this view lightgray and slightly transparent. You can do that with the following:
UIView *separatorView = [[UIView alloc] initWithFrame:CGRectMake:
(0, cell.frame.size.height-1,
cell.frame.size.width, 1)];
[separatorView setBackgroundColor:[UIColor lightGrayColor]];
[separatorView setAlpha:0.8f];
[cell addSubView:separatorView];
The width of this view is 1 pixel which is the same as the default separator, it runs the length of the cell, at the bottom.
Since cellForRowAtIndexPath is only called as often as you have specified in numberOfRowsInSection these subviews will only be created for the cells that possess data and should have a separator.
Hope this helps.
This worked for me - I had extra empty rows at the bottom of the screen on an iphone 5 -
In my case I needed 9 rows
- (CGFloat)tableView:(UITableView *)tabelView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return self.tableView.frame.size.height / 9;
}
You can implement heightForRowAtIndexPath: and compute the correct height to only show 5 cells on the screen.
Are you always going to have 5 rows? If it's a dynamic situation you should set the number of rows according to the datasource of the tableview. For example:
return [postListData count];
This returns the count of the records in the array holding the content.
The tableview is only going to display the number of rows and sections that you tell it to. If you're always going to have just a single section, DON'T implement the method below.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
Without this the tableview will only have 1 section. With it, as you would imagine, you can specify the number of sections.
It is quite Simple. Just set the size of the popover like this:
self.optionPickerPopOver.popoverContentSize = CGSizeMake(200, 200);
Certainly you can adjust the size (200,200) depending upon the size of contents and number if rows.
Easy way would be to shrink tableView size. I.e. 5 cells 20 points each gives 100.0f, setting height to 100.0f will cause only 5 rows will be visible. Another way would be to return more rows, but rows 6,7 and so would be some views with alpha 0, but that seems cumbersome. Have you tried to return some clerColor view as footerView?
I think u can try changing the frame of the table view, if you want to adjust with the number of cells.
Try something like this:
[table setFrame:CGRectMake(x, y, width, height*[list count])];
height refers to height of the cell
As Nyx0uf said, limiting the size of the cell can accomplish this. For example:
- (CGFloat)tableView:(UITableView *)tabelView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat result;
result = 100;
return result;
}
implement these two methods in your UITableViewController:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if (section == tableView.numberOfSections - 1) {
return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (section == tableView.numberOfSections - 1) {
return 1;
}
return 0;
}
In fact, these codes are telling tableview that you don't need to render the seperator line for me anymore, so that it looks the empty cells won't be displayed(in fact , the empty cell can not be selected too)

Change image or style of specific UITableViewCell

Is there any way that I can get a specific cell to change its style or background image while scrolling the table view?
I want to change the image of a cell which is on top of the visible cells. But only its image is going to be changed the others will stay same, until after scrolling the others come to top of the cells which are shown on the screen. Then the top one's image is going to change this time.
You need to implement scrollViewDidScroll: method of UIScrollViewDelegate in your controller then use visibleCells property of UITableView to get the visible cells of the table.
Something like the following code should work
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
UITableView* tableView;
NSArray* visibleCells;
BOOL first;
tableView = (UITableView*)scrollView;
visibleCells = tableView.visibleCells;
first = YES;
for (UITableViewCell* cell in visibleCells) {
if (first) {
//customize the top cell
first = NO;
}else {
//customize the other visible cells
}
}
}

UILabel backgroundColor in UITableViewCell always reverts to initial value

In my app, I have a UITableViewCell which is used to display a background color setting. In the detailTextLabel, it displays the name of the color with the background set to the actual color, e.g. [UIColor cyanColor]. Note that I am setting the background of the detailTextLabel only, not the whole UITableViewCell. When users tap on the cell they are taken to another UITableView which lets them choose a color, and when they return to the previous UITableView the backgroundColor of the UILabel is updated to the new color.
The problem is, whenever I return to the initial UITableView, the UILabel's backgroundColor updates momentarily and then returns to the initial color. I cannot find out why it would be reverting. Any suggestions?
Thank you!
Some state-based properties are set by the table view; I believe that background color is one of them. In other words, the table view is changing the background color of detailTextLabel, probably as part of unhighlighting the selection.
After the table view sets state-based properties, the table delegate is given a final chance to update the appearance of each cell. This is done in the delegate's tableView:willDisplayCell:forRowAtIndexPath: method. Perhaps if you set the background color of detailTextLabel in this method your problem will go away.
When cellForRowAtIndexPath executes, it typically creates and returns a new cell.
From your question, it is unclear if you are recreating the cell or not, but if you are, this could explain the behavior you describe.
Yes..Maybe you are not re-using your cells in cellForRowAtIndexPath methode.
If it is, try to re-use your cells rather than creating new one everytime.
The way that I fixed this was to create a UILabel subclass called HighlightedLabel which has the following initialiser:
- (id)initWithHighlightedBackgroundColor:(UIColor *)highlightedBackgroundColor nonHiglightedBackgroundColor:(UIColor *)nonHighlightedBackgroundColor
{
self = [super init];
if(self)
{
_highlightedBackgroundColor = highlightedBackgroundColor;
_nonHighlightedBackgroundColor = nonHighlightedBackgroundColor;
self.backgroundColor = nonHighlightedBackgroundColor;
}
return self;
}
-(void)setHighlighted:(BOOL)highlighted
{
if(highlighted)
{
self.backgroundColor = self.highlightedBackgroundColor;
}
else
{
self.backgroundColor = self.nonHighlightedBackgroundColor;
}
}
Then when I allocate this cell I specify the highlighted and non-highlighted background colour.
This works perfectly - when I select the cell the colour is what I want.

Calculate custom cell height with multiple views iphone

I want to calculate my custom cell height with multiple subviews like labels, imageViews, buttons etc.
How do I calculate this cell size?
It seems it's not recommended to do it in cellForRowAtIndxPath, also taking an instance of Cell in heightForRowAtIndexPath, it goes in infinite loop.
I'm able to calculate the height for cell in the custom cell class but I'm not sure wheather where should I return it.
Since the method, heightForRowAtIndexPath doesn't get a cell instance, how to tell this method the type of cell for which I want to render the calculated height?
Any help is appreciated.
Thanx in advance.
Ideally, you should be able to calculate the height of the cell without having to create a cell for it. tableView:heightForRowAtIndexPath: will be called for every cell in your table (not just the ones on screen) and if you have to create a complete cell object in that method it will probably have a big impact on performance.
You have to derive the type of cell based on the indexPath argument, in the same way you do it in tableView:cellForRowAtIndexPath:
You could always load your cells in viewWillAppear or viewDidLoad, and then add them to an NSArray. Then, in tableView:heightForRowAtIndexPath:, using the provided indexPath, return the height of the relevant cell in your array. Then, in tableView:cellForRowAtIndexPath, again, using the provided indexPath, return the relevant cell in your array.
PLEASE NOTE:
If you have a lot of cells, this could cause your UI to lock up while it loads the cells. To avoid this, you could create a custom method to populate the array, and perform it on a background thread.
Also, if it takes too long, the tableView will not display all of your cells, as your array will not have been fully populated. This means that you should call [tableView reloadData]; after the array is populated.
This' how I did it:
I created an array of catgories of the cells in the order of their apearence in tableview called category_type and then I checked for the category types and calculated the height of the text-based controls to calculate the entire height of cell.
NSString *category = [NSString stringWithFormat:[category_type objectAtIndex:indexPath.row]];
if([category isEqualToString:#"xyz"])
{
xyzInstance = [arrayWithObjectsToLoad objectAtIndex:indexPath.row];
float textHeight = [MLUtils calculateHeightOfTextFromWidth:editorialInstance.eletitle : [UIFont fontWithName:#"Helvetica" size:13.0] :320 :UILineBreakModeWordWrap]; //Dyanamic label height
totalHt = 171 - 21 + textHeight + 20; // My calculation for text part of label
return totalHt;
}
I did this for all the categories.