custom tableview cells with different background images for ios application - ios

I want to design a tableview such that each cell has a different background image depending on a condition. There are 10 images for cell background and I want to load them as such that the image pattern is repeated on the cells. For example, cells 1-10 take images from 1-10 respectively. Then cells 11-20 also take images 1-10 and so on. How can I achieve this?

give image name like: images_1/2.../10.png
//write it
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell1";
CustomCell *Cell = (CustomCell *)[tabeleYourTurn dequeueReusableCellWithIdentifier:CellIdentifier];
if (Cell == nil)
{
NSArray *topLevelObject= [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
}
for (id currentobject in topLevelObject)
{
if ([currentobject isKindOfClass:[UITableViewCell class]])
{
Cell = (CustomCell *) currentobject;
break;
}
}
}
NSString *image = [NSString stringWithFormat:#"images_%d.png", indexPath.row % 10 + 1];
Cell.imageView.image = [UIImage imageNamed:image];
return Cell;
}
May be it will work.

First create an array with ImageName..
Like
NSArray *ImageArray=[[NSArray alloc]initWithObjects:#"1.png",#"2.png"];
Then this array with 10 images
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// create a background image for the cell:
UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectZero];
[cell setBackgroundColor:[UIColor clearColor]];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell setBackgroundView:bgView];
[cell setIndentationWidth:0.0];
((UIImageView *)cell.backgroundView).image = [UIImage imageNamed:[ImageArray objectAtIndex:indexPath.row%10]];
}

Assuming that your image names are 1, 2...10, you can try to do something like this:
NSString *imageName = [NSString stringWithFormat:#"%d", indexPath.row % 10 + 1];
Than you can use this imageName where you want.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if( (indexPath.row % 10 ) <= 1)
{
// set your image
cell.imageView.image = [UIImage imageNamed:#"image1.png"];
}
}

Related

Tableview images chaging when scrolling using custom tableview

I have a Tableview and i am using the customTableview cell for displaying the cell
in the cell i have 5 buttons (rating buttons) when i click on particular button the image of the button has to changed its working fine but when i am scrolling the tableview again thay are changing to normal rating buttons see the following images for clarity
This is the image before scrolling and clicking nothing on button
after clicking the on rating buttons the image is changing like this
but when scrolling the cells again its changing to first image
please help me out
Code :
- (UITableViewCell )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
dataCell = (DataCel)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (dataCell==nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"DataCel" owner:self options:nil]; dataCell = [nib objectAtIndex:0];
}
return dataCell;
}
you should save the sate of the button images
and in your
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
insert code to keep them updated
for ex:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//create your cell here if it was not created
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
[cell.textLabel setFont:[UIFont boldSystemFontOfSize:13]];
[cell.textLabel setTextColor:[UIColor darkGrayColor]];
[cell.detailTextLabel setFont:[UIFont boldSystemFontOfSize:11]];
[cell.detailTextLabel setTextColor:[UIColor lightGrayColor]];
cell.detailTextLabel.numberOfLines = 2;
}
NSArray *array = [imageArray objectAtIndex:[indexPath row]];
if([[array objectAtIndex:3] isEqual:#"rate3"])
{
//set the code for the rating 3
}
else
{
//insert another rate depending on your object
}
This is because every time you scroll the UITableView, it refreshes its values from the data you provided it ! So the value is not retained. To prevent this from happening every time you scroll the database you can use this
static NSString *cellIdentifier = #"Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
}
for(id obj in cell.contentView.subviews)
{
if([obj isKindOfClass:[UIImageView class]] || [obj isKindOfClass:[UILabel class]])
{
[obj removeFromSuperview];
}
}
This would not let the values be overlapped.
When scrolled the cells are recreated and so you need the keep the selected values in a seperate array and set it in the cellForRowAtIndexPath method
This happens because every time you scroll the tableview , it re-create tableview cell. therefor you can use array for reused it.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"cellId";
UITableViewCell *cell = [categoriesTableview dequeueReusableCellWithIdentifier:cellId];
if (cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
// ui goes here
return cell
}

UITableView scrolling delay

Found The solution with help of #parcs and #payal- This link helped -> http://www.appcoda.com/customize-table-view-cells-for-uitableview/
Basically , the table view lags when i scroll, i have gone through many post but it didn't help-
UITableView in UIScrollview scrolling delay
Delayed UIImageView Rendering in UITableView
slow scrolling of UITableView
UITableView lags while scrolling
This is the code , which i am using in cellForRowIndexpath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"WorkoutCell";
WorkoutCell *cell = (WorkoutCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
UIViewController *controller=[[UIViewController alloc] initWithNibName:CellIdentifier bundle:nil];
cell=(WorkoutCell *)controller.view;
}
[[cell contentView] setBackgroundColor:[UIColor clearColor]];
[[cell backgroundView] setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundColor:[UIColor clearColor]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//diplaying Workout Data
[cell setupFont];
cell.lblPoint.text=[NSString stringWithFormat:#"%#",[[arrTblData objectAtIndex:indexPath.row]valueForKey:#"point"]];
cell.lblReps.text=[[arrTblData objectAtIndex:indexPath.row]valueForKey:#"reps"];
cell.lblWorkoutCategory.text=[[arrTblData objectAtIndex:indexPath.row]valueForKey:#"workout"];
cell.lblWeight.text=[[arrTblData objectAtIndex:indexPath.row]valueForKey:#"weight"];
cell.lblSets.text=[[arrTblData objectAtIndex:indexPath.row]valueForKey:#"sets"];
//Formatting Date
cell.lblPostedDate.text=[NSString stringWithFormat:#"Posted On: %#",[self formatDate:[[arrTblData objectAtIndex:indexPath.row]valueForKey:#"time"]]];
return cell;
}
The Function SetupFont called from cellForRowIndexpath
-
(void)setupFont
{
self.lblPoint.font=DEFAULT_FONT(13);
self.pointLblScreen.font=DEFAULT_FONT(13);
self.lblPostedDate.font=DEFAULT_FONT(13);
self.lblReps.font=DEFAULT_FONT(13);
self.lblSets.font=DEFAULT_FONT(13);
self.lblWeight.font=DEFAULT_FONT(13);
self.lblWorkoutCategory.font=DEFAULT_FONT(18);
self.lblWorkoutCategory.strokeColor=kStrokeColor2;
self.lblWorkoutCategory.strokeSize = kStrokeBigSize;
for(UILabel *lbl in [self subviews])
{
if(lbl.tag==9)
{
lbl.font=DEFAULT_FONT(13);
}
}
}
Format Date Function called from cellForRowIndexpath
#pragma mark - Format Date Function
-(NSMutableString*)formatDate:(NSString*)date
{
NSMutableString *formattedDate=[[NSMutableString alloc]init];
NSUInteger p = 0;
NSUInteger length = [date length];
while ( p < length) {
unichar ch = [date characterAtIndex: p];
if (ch!=' ')
{
if(ch=='-')
{
ch='/';
}
NSString *appendString = [NSString stringWithFormat:#"%c",ch];
[formattedDate appendString:appendString];
p++;
}
else
{
break;
}
}
return formattedDate;
}
This is not the right way to do this:
WorkoutCell *cell = (WorkoutCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
UIViewController *controller=[[UIViewController alloc] initWithNibName:CellIdentifier bundle:nil];
cell=(WorkoutCell *)controller.view;
}
Instead of this, subclass UITableViewCell and instantiate that cell here as follows:
WorkoutCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"WorkoutCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
Also, move your setupFont method the UITableViewCell subclass.
You are typecasting the UIViewController to UITableviewCell.Instead of that take UITableViewCell class add the outlets whatever you want and use it
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle]loadNibNamed:#"CustomCell" owner:nil options:nil] objectAtIndex:0];
}
// access that CustomCell variables here
cell.name.text = #"Name";
}
You use
#interface WorkoutCell : UITableViewCell {}
then table view class
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString * const CellIdentifier = #"WorkoutCell";
WorkoutCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if( !cell )
{
cell = [[WorkoutCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
}
}

Changing specific row in table from void

I have a populated TableView where i would like to change the image. When I'm inside cellForRowAtIndexPath I can change it by using:
cell.imageView.image = [UIImage imageNamed:#"image.png"];
But I cannot figure out how to do the same thing inside the void, I have tried:
[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]].imageView.image = [UIImage imageNamed:#"image.png"];
But it doesn't change the image at all.
Thanks,
/DSDeniso
EDIT:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
_row = indexPath.row;
[self changeImage];
}
- (void)changeImage{
UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_row inSection:0]];
if(cell)
{
cell.imageView.image = [UIImage imageNamed:#"image.png"];
} else {
NSLog( #"why is cell null? Did I set row properly?");
}
}
Just pass the cell to the method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
_row = indexPath.row;
[self changeImageForCell:cell];
}
- (void)changeImageForCell:(UITableViewCell*)cell{
cell.imageView.image = [UIImage imageNamed:#"image.png"];
}

IOS centered UIImages in a row

I have a little problem. I want to display multiple (amount is dynamic) UIImages in a UITableViewCell (in the center).
How can I do that (UIImages are generated programmatically)?
Thanks a lot!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
self.imgThings = [[UIImageView alloc] init];
// imgThings.backgroundColor= [UIColor magentaColor];
[self.imgThings setImage: [UIImage imageNamed:#"image.png"]];
self.imgThings.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);
[cell.contentView addSubview:self.imgThings];
}
return cell;
}
Above is simple example for only one image, u can also set it for multiple image by changing name of image.

Iteration in UITableView for a particular cell

I have data and image in cells of UITableView. I want to change image of a particular cell,
I have a code with each cell.
I am not able to find the way to iterate the tableview for cell.
here is snap shot of my tableview function.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellText = nil;
NSArray *keys = [[[self packageItems] allKeys]
sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
cellText = [keys objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
cell.imageView.image=[UIImage imageNamed:#"checkbox.png"];
//cell.imageView.image=[UIImage imageNamed:#"checkbox-checked.png"];
return cell;
}
There is another function where i want to change cell image to cell.imageView.image=[UIImage imageNamed:#"checkbox-checked.png"];
- (void)OnAction
{
static NSString *CellIdentifier = #"Cell";
NSIndexPath *indexPath = [[NSIndexPath alloc] initWithIndex:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath ];
cell.imageView.image=[UIImage imageNamed:#"checkbox-checked.png"];
}
If you wont to change something in table view with you function you dont have to make new cell, just modify actual cell. Just make condition in your cellForRowAtIndexPath delegate:
if(indexPath == indexToModify) {
cell.imageView.image=[UIImage imageNamed:#"checkbox-checked.png"];
} else {
cell.imageView.image=[UIImage imageNamed:#"checkbox.png"];
}
and in your - (void)OnAction method use in the end:
[self.tableView reloadData];

Resources