Conditional background color for UITableViewCell - ios

I'm trying to set the background color for a subclassed UITableViewCell. I know that normally I'm supposed to use tableView:willDisplayCell:forRowAtIndexPath:. However, I have some logic to customize several aspects of the UITableViewCell in tableView:cellForRowAtIndexPath:, and I would like to set the background color of the cell based on that logic. Do I have to duplicate the whole set of conditions I have in tableView:cellForRowAtIndexPath: in tableView:willDisplayCell:forRowAtIndexPath: just to set the background color? That kind of code repetition seems wasteful and probably unwise.
The best solution I can think of is to add a UIColor property to the UITableViewCell subclass, set it in tableView:cellForRowAtIndexPath: based on the conditions there, and just have tableView:willDisplayCell:forRowAtIndexPath: set the cell's background color to its own color property. Is this the right way to handle this situation? It seems kinda clunky, but I can't think of anything better.

You can create function returning appropriate color and set it accordingly like
-(UIColor*)getColorForIndexPath:(NSIndexPath*){
UIColor *cellColor=nil;
//Do whatever your logic is for finding appropriate color assign to cellColor
return cellColor;
}
Now use this function to setcolor as, so you have to modify only 1 place to effect at both place.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
.... //Your code for cell
cell.backgroundColor = [self getColorForIndexPath:indexPath];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
.... //Your code for cell
cell.backgroundColor = [self getColorForIndexPath:indexPath];
}
Note : This will still work if you place only at any of the delegate method, so no need to call at 2 places.

Related

UItableviewcell Reuse issue in ios

Hello guys i think almost everyone who is in ios development may come across the issue of reuse of the UITableCell by using following code line.
RZRestaurantListingCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
I have search lot about this but not getting any desire answer so please help me out in this case.
I have the same issue as most of iPhone developer having after reusing the cell.
I have the UIProgressView inside my cell and one button is there for downloading the video and i am showing the progress there in progress view how much is left.
So Now what i have problem is when i have more data and going out of the screen at that time i am press the download button on very first row of the UITableviewCell then i am scrolling down so the progress also shown in bottom random one cell so the UI changes in two cell rather then one.
You need to implement -prepareForReuse method in your custom cell class and set all cell properties to default value.
- (void)prepareForReuse
If a UITableViewCell object is reusable—that is, it has a reuse
identifier—this method is invoked just before the object is returned
from the UITableView method dequeueReusableCellWithIdentifier:. For
performance reasons, you should only reset attributes of the cell that
are not related to content, for example, alpha, editing, and selection
state. The table view's delegate in tableView:cellForRowAtIndexPath:
should always reset all content when reusing a cell. If the cell
object does not have an associated reuse identifier, this method is
not called. If you override this method, you must be sure to invoke
the superclass implementation.
Refer here for more, https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/#//apple_ref/occ/instm/UITableViewCell/prepareForReuse
You need to assign a progress value inside the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RZRestaurantListingCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// If the cell is reused, the `-prepareForReuse:` of `UITableViewCell` will be called.
//!! Assign current progress value to this cell, otherwise, the progressBar.value may look like a random value.
//!! Because the current cell is reused from a disappeared cell.
cell.progressBar.value = ... ;
return cell;
}
The design may be complex, because the progress may be updated continuously when the cell is on the screen.
Use prepareforreuse method to clear content of cell before using it... e.g.
-(void)prepareForReuse
{
[super prepareForReuse];
self.textLabel.text = #"";
self.detailTextLabel.text = #"";
self.imageView.image = nil;
}

Issue in reusing table view cells

My custom cell contains many ui elements. I found it very difficult to reuse cells.So i set the cell created to nil in the didEndDisplayingCell delegate method of table view. Is there any issue in doing so. will it hamper performance. This is my code
(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ([tableView.indexPathsForVisibleRows indexOfObject:indexPath] == NSNotFound){
VBMerchantDealCell *cell = (VBMerchantDealCell *)[tableView cellForRowAtIndexPath:indexPath];
cell = nil;
}
}
Make use of the prepareToReuse method in your cell subclass. Here reset all the values to default and in the corresponding cellForRowAtIndexPath method set the appropriate values to cell.
And ya, not reusing the cell is an unnecessary performance overhead.
Yes, not reusing cells will be very bad for performance. Also using transparency in your cells is likely to affect performance (i.e. Scrolling). You can use instruments or the simulator to check for this: 'color blended layers' will color all non-opaque views as red.

UITableView - Cells not colored on initial load?

I am implementing UITableViewDelegate and UITableViewDataSource in my VC.
I have this code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *) indexPath {
// do stuff
if(condition){
cell.textLabel.backgroundColor = [UIColor redColor];
}
else {
cell.textLabel.backgroundColor = [UIColor blackColor];
}
}
Pretty standard stuff. The issue I am running into is that the first set of visible cells has no coloring. If I scroll to cells that aren't visible, I get correctly colored cells. If I scroll back, I the cells are colored.
I understand that the cells are 'created' and 'destroyed' as needed, which explains some of it, I just doesn't understand why cellForRowAtIndexPath, which is fired before I see anything, and my coloring conditionals being hit, results in... no coloring.
A work around is to iterate through the visible cells after calling reloadData on the table view, I am just hoping there is an easier way.
Dane
Cell coloring needs to be done in the tableView:willDisplayCell:forRowAtIndexPath: delegate method, not the tableView:cellForRowAtIndexPath: method.
Remove any cell coloring logic from the tableView:cellForRowAtIndexPath: method and move it to the tableView:willDisplayCell:forRowAtIndexPath: method.

Is this an example of an iOS UITableView?

I need to do something like this :
.
Is it a UITableView? Or how should I do this? Any idea is welcome.
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
if (indexPath.row == 0) return cell1;
} else if (indexPath.section == 1) {
if (indexPath.row == 0) return cell2;
} else if (indexPath.section == 2) {
if (indexPath.row == 0) return cell3;
}
}
Most likely its a UITableView. May be its not (it can be a UIScrollView with views added like sections in a table view).
But if you want to create something like in the image, I'd suggest you to use UITableView with customized cells. Because implementing table view, in this case, is simpler as you have to just concern about the delegate and the dateSource of the table view, rather than worrying about aligning the views in order.
Use sectioned table view with the translucent, gray-bordered, black-background-view as the backgroundView of the cells. Add the labels and arrow as the subviews of cell. You can add arrow as the accessoryView but that would become vertically centered, but in the image the arrow is displayed slightly on top of the cell.
There should be only one cell in each section.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
There can be any number of sections (3 here).
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3; // Can be any number
}
Yes, that's most likely a styled table view. See this guide or this nice tutorial.
why you worry about this one? Its simple man.... its nothing ,if you have hands over UITableView. Yeah, You must know about :
1: Set background image on view,exact as table size.
2: Set table view onto them and make background transparent.
3: Make customize table cell and add to table' row.
Thats it.....enjoy this work..its a creativity.
And you can do it guy....
I cannot tell exactly what is used from the picture, however I suggest using the UITableView especially if you have variable data coming from a structured object.
Some tutorials which I found very useful are the following:
Creating customized UITableViewCell from XIB
http://www.bdunagan.com/2009/06/28/custom-uitableviewcell-from-a-xib-in-interface-builder/
UIViewTable anti-patterns - A very good MVC pattern to build UIViewTable
http://www.slideshare.net/nullobject/uitableviewcontroller-antipatterns
And of course Apple's docs:
http:/developer.apple.com/library/ios/#DOCUMENTATION/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html
One important fact to remember is that you should always reuse cached table cells through the datasource method tableView:didSelectRowAtIndexPath:
Note: Since you want lots of transparencies there might be some performance issue especially on older devices.
Hope this helps and goodluck with your project.
There is not need to its scrollview or tableview... both are perform same..
Just change the Background Color of Tableview as clear color.....
check this code...
put this line to viewDidLoad :
[tableView setBackgroundColor:[UIColor clearColor]];
and
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
..... // Your label buttons etc........
......
......
.....
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell setClipsToBounds:YES];
[[cell layer] setBorderColor:[[UIColor blackColor] CGColor]];
[[cell layer] setCornerRadius:10];
}
You can do this using tableView in
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//here place return your array contents
}
We can accomplish the similar as shown in image above by Table View and scroll view both.If you don't want to use table view then you can take a scroll view or a simple view and can add several buttons along with the image in background with different frames.It means button background will have the same image.then you can use different labels to show all the texts in different labels according to the requirement.
OR
you can use table view.For this you need to return number of sections 3(in this image) in the delegate method - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView.and then everything as the default tableview style.And then change the cell style to detailed.
yes this is a tableView with custom UITableViewCell.you can do that by creating a empty xib file and then add a UITableViewCell and do your customizing ...then in cellForRowAtIndexPath method use:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:edentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle]loadNibNamed:#"yourCellName" owner:self options:nil]objectAtIndex:0];
this is from my application.
Yes, it is a groupedtableview with clearcolor as backgroundcolor. Cell background color also needs to be changed to clearcolor.
I suggest using div or list, don't use table, its very hard to control if the contents are too long.

How to set color in empty UITableViewCell?

I want to replicate Gowalla UI of the detail part (the table with Map & Directions) picture below
I replicate it with a table with a contentInset, set backgroundColor to clearColor, and change color of UITableViewCell in - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath to white, but the result
isn't what I expected the empty cells are white only the cells with content (a number return by - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section ) here is a result I got
Is there any way to set empty cell color or any guide on how to achieve this ?
Set background color of view same as you are setting for your cell. and make cell transparent.
Due to no response for long time I would posted my solution here. I solved this problem by having UITableView under UIView and set parent view background color the same as UITableViewCell color.

Resources