I want to change the background image of the cell when cell it is selected.
I have tried with this syntax:
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
//for adding image
cell.ImgCateg.image=[UIImage imageNamed:[ImgArr objectAtIndex:indexPath.row]];
...
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CRHomeCategCell *cell = (CRHomeCategCell*)[_tblCateg cellForRowAtIndexPath:indexPath];
cell.ImgCateg.image = [UIImage imageNamed:#"DefaultBg.png"];
}
this changes image in the foreground not in the background.
Please help and thanks in advance.
You can set UITableviewCell's property setSelectedBackgroundView in cellForRowAtIndexPath method.
like ,
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
{
....
UIImageView *normalCellBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
[normalCellBG setImage:[UIImage imageNamed:#"box_nonselected.png"]];//Set Image for Normal
[normalCellBG setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundView:normalCellBG];
UIImageView *selecetedCellBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
[selecetedCellBG setImage:[UIImage imageNamed:#"box_selected.png"]];//Set Name for selected Cell
[selecetedCellBG setBackgroundColor:[UIColor clearColor]];
[cell setSelectedBackgroundView:selecetedCellBG ];
}
look this SO question.
You can do this on following way:
UIImage *cellImage = [UIImage imageNamed:#"myimage.png"];
UIImageView *cellView = [[UIImageView alloc] initWithImage:cellImage];
cellView.contentMode = UIContentViewModeScaleToFill;
cell.backgroundView = cellView;
Related
I want to show image in full screen when user tap on it.
Can anyone help me please?
My ImageView is in TableViewCell.
Here is what i've tried:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
ImageListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.propertyImageView.frame = [UIScreen mainScreen].bounds;
cell.propertyImageView.image = [imageList objectAtIndex:indexPath.row];
}
It is quite easy like this:
-(void)removeImage {
UIImageView *imgView = (UIImageView*)[self.view viewWithTag:100];
[imgView removeFromSuperview];
}
-(void)addImageViewWithImage:(UIImage*)image {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:self.view.frame];
imgView.contentMode = UIViewContentModeScaleAspectFit;
imgView.backgroundColor = [UIColor blackColor];
imgView.image = image;
imgView.tag = 100;
UITapGestureRecognizer *dismissTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(removeImage)];
dismissTap.numberOfTapsRequired = 1;
[imgView addGestureRecognizer:dismissTap];
[self.view addSubview:imgView];
}
Call from your table delegate like this:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self addImageViewWithImage:[imageList objectAtIndex:indexPath.row]];
}
I created mycell,and in myTableview I created a
#property (strong ,nonatomic)NSArray *contentPictureArray;
this is:
self.contentPictureArray = #[[UIImage imageNamed:#"wallha#2x.png"],#"wallha#2x.png"],[UIImage imageNamed:#"wallha#2x.png"]];
I want to use cell.contentPictureView.view=[self.contentPictureArray objectAtIndex:indexPath.row]; but not true ,how can I add mycell to the cellForRowAtIndexPath?
cell.stateView = [[UIView alloc] initWithFrame:CGRectMake(XB, HeiB+NY, WidB, 0)];
[cell addSubview:cell.stateView];
cell.contentTextView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WidB, 0)];
[cell.stateView addSubview:cell.contentTextView];//动态文字部分
cell.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WidB, 0)];
[cell.contentTextView addSubview:cell.contentLabel];//动态文字label
cell.allContentBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, cell.contentLabel.frame.size.height, HeiB, 2*NX)];
[cell.contentTextView addSubview:cell.allContentBtn];//全文显示按钮
cell.contentPictureView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.stateView.frame.origin.y+2*NX, HeiB+NY, 0)];
[cell.stateView addSubview:cell.contentPictureView];
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
KBTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = [[KBTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdent:#"cell"];
}
if(indexPath.row==0||indexPath.row==1){
cell.iconImageView.image = [UIImage imageNamed:#"avatar.png"];
}
cell.contentLabel.text=[self.listArray objectAtIndex:indexPath.row];
cell.nameLabel.text = [self.nameArray objectAtIndex:indexPath.row];
cell.timeLabel.text =[self.timeArray objectAtIndex:indexPath.row];
cell.contentPictureView.=[self.contentPictureArray objectAtIndex:indexPath.row];
return cell;
}
I don't understand.
In your code:
[self.contentPictureArray objectAtIndex:indexPath.row] is UIImage
cell.contentPictureView.view is UIView ?
so you code:
cell.contentPictureView.view = [self.contentPictureArray objectAtIndex:indexPath.row]
how you can do it?
Add the image like this in your cellForRowAtIndexPath
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10,100, 200)];
imageView.backgroundColor = [UIColor clearColor];
imageView.image = [contentPictureArray objectAtIndex:indexPath.row] ;
[cell.contentView addSubview:imageView];
Hope this helps...
You can initialize array with image name only like below.
self.contentPictureArray = #[#"wallha#2x","wallha#2x",#"wallha#2x"]];
Now use below code:
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10,100, 100)];
imgView.backgroundColor = [UIColor clearColor];
imgView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[contentPictureArray objectAtIndex:indexPath.row] ofType:#"png"]] ;
[cell.contentView addSubview:imageView];
Also if you want you can create a custom cell also.
I have a UITableView with paging enabled. At first I can scroll very smoothly between cells. However, after scrolling back and forth a few times, it becomes rather choppy. Am I not creating/reusing my cells properly?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cash-Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 40, cell.frame.size.width-40, cell.frame.size.width-40)];
Purchase *purch = (Purchase *)self.purchases[indexPath.row];
[image setImage:[UIImage imageWithData:purch.image]];
image.contentMode = UIViewContentModeCenter;
image.contentMode = UIViewContentModeScaleAspectFill;
image.clipsToBounds = YES;
[self setMaskTo:image byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
[cell addSubview:image];
UIView *price = [[UIView alloc] initWithFrame:CGRectMake(20, 40+cell.frame.size.width-40, cell.frame.size.width-40, 60)];
UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, price.frame.size.width, price.frame.size.height)];
[priceLabel setText:#"$10.0"];
[priceLabel setTextAlignment:NSTextAlignmentCenter];
[priceLabel setFont:[UIFont boldSystemFontOfSize:25.0f]];
[self setMaskTo:price byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight];
[price addSubview:priceLabel];
[price setBackgroundColor:[UIColor whiteColor]];
[cell addSubview:price];
return cell;
}
If you want to reuse them then make sure you add the content to your UITableViewCell only once and on reusing it just update it (e.g. update the image and the price accordingly).
Otherwise, what you get with your implementation is adding subviews price and image everytime (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath gets called. Just try debugging the number of subviews your cell has to see if I am right.
You could go this way:
if ( ! [cell viewWithTag: 1])
{
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 40, cell.frame.size.width-40, cell.frame.size.width-40)];
image.tag = 1;
image.contentMode = UIViewContentModeCenter;
image.contentMode = UIViewContentModeScaleAspectFill;
image.clipsToBounds = YES;
[self setMaskTo:image byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
[cell addSubview:image];
}
if ( ! [cell viewWithTag: 2])
{
UIView *price = [[UIView alloc] initWithFrame:CGRectMake(20, 40+cell.frame.size.width-40, cell.frame.size.width-40, 60)];
UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, price.frame.size.width, price.frame.size.height)];
priceLabel.tag = 2;
[priceLabel setTextAlignment:NSTextAlignmentCenter];
[priceLabel setFont:[UIFont boldSystemFontOfSize:25.0f]];
[self setMaskTo:price byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight];
[price addSubview:priceLabel];
[price setBackgroundColor:[UIColor whiteColor]];
[cell addSubview:price];
}
Purchase *purch = (Purchase *)self.purchases[indexPath.row];
UIImage *image = [cell viewWithTag: 1];
[image setImage:[UIImage imageWithData:purch.image]];
UILabel *priceLabel = [cell viewWithTag: 2];
[priceLabel setText:#"$10.0"];
This is because - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is being called each time the cell comes into the view. Add the images to an array (in viewDidLoad or somewhere), and you'll get a smoother result.
You are not reusing the cells, which causes the lag/choppy scrolling.
suggestion:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cash-Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(cell == nil){
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 40, cell.frame.size.width-40, cell.frame.size.width-40)];
Purchase *purch = (Purchase *)self.purchases[indexPath.row];
[image setImage:[UIImage imageWithData:purch.image]];
image.contentMode = UIViewContentModeCenter;
image.contentMode = UIViewContentModeScaleAspectFill;
image.clipsToBounds = YES;
image.tag = 10;
[self setMaskTo:image byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
[cell.contentView addSubview:image];
UIView *price = [[UIView alloc] initWithFrame:CGRectMake(20, 40+cell.frame.size.width-40, cell.frame.size.width-40, 60)];
UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, price.frame.size.width, price.frame.size.height)];
[priceLabel setText:#"$10.0"];
[priceLabel setTextAlignment:NSTextAlignmentCenter];
[priceLabel setFont:[UIFont boldSystemFontOfSize:25.0f]];
[self setMaskTo:price byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight];
[price addSubview:priceLabel];
[price setBackgroundColor:[UIColor whiteColor]];
price.tag = 11;
[cell.contentView addSubview:price];
}
//update the value here
UIImageView *imgV = (UIImageView *)[self.contentView viewWithTag:10];
// update the imageView here
//imgV.image = image
UIView *vv = (UIView *)[self.contentView viewWithTag:11];
// Update the UIView here
//vv.text = #"text here";
//Put setselection style here to allow the cell to be loaded before setting the property value
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
This question already has answers here:
UITableView Separator line
(14 answers)
Closed 9 years ago.
In my table view, I need to set the cell separator as an image that I have with me. How can I do this?
A simple solution is you can set a pattern image as cell seperator.
[tableView setSeparatorColor:[UIColor colorWithPatternImage:[UIImage imageNamed:...]]];
Or you can simply add a UIImageView in the cell content view
UIImageView *imagView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"seprater.png"]];
imgView.frame = CGRectMake(0,cell.contentView.frame.size.height - 1.0, cell.contentView.frame.size.width, 1);
[customCell.contentView addSubview:imageView];
Your best bet is probably to set the table's separatorStyle to UITableViewCellSeparatorStyleNone.
And manually adding/drawing a line (perhaps in tableView:cellForRowAtIndexPath:) when you want it.
In tableView:cellForRowAtIndexPath:
...
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
// Drawing our own separatorLine here because I need to turn it off for the
// last row. I can only do that on the tableView and on on specific cells.
// The y position below has to be 1 less than the cell height to keep it from
// disappearing when the tableView is scrolled.
UIImageView *separatorLine = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, cell.frame.size.height - 1.0f, cell.frame.size.width, 1.0f)];
separatorLine.image = [[UIImage imageNamed:#"grayDot"] stretchableImageWithLeftCapWidth:1 topCapHeight:0];
separatorLine.tag = 4;
[cell.contentView addSubview:separatorLine];
[separatorLine release];
}
// Setup default cell settings.
...
UIImageView *separatorLine = (UIImageView *)[cell viewWithTag:4];
separatorLine.hidden = NO;
...
// In the cell I want to hide the line, I just hide it.
seperatorLine.hidden = YES;
...
In viewDidLoad:
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
to active this put an image view at the bottom of your cell and assign an image to it .also please make sure you have set cellSeperatorStyle as None
That should work
{ [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"<image name>"]];
imgView.frame = CGRectMake(0, cellHeight, cellWidth, 1);
[customCell.contentView addSubview:imgView];
return customCell;
}
}
First of all you have to make SeparatorStyle to none by following code
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
after that add image too the end of every cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
.....
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"seprater.png"]];
[imgView sizeToFit];
[Cell.contentView addSubview:imgView];
.....
}
I am using a separate UITableViewCell for the UITableView and when i set background View of this cell, it seems to fill the image in a box rather than the whole cell. The dimensions of both separate UITableViewCell and UITableView's row are 280x44. Here's an image of what it looks like:
Here's the code that i am writing for setting the background view of the cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// ...
PBDashboardSummarizedCell *cell = (PBDashboardSummarizedCell *)[tableView dequeueReusableCellWithIdentifier:#"PBDashboardSummarizedCell"];
if (!cell) {
cell = [PBDashboardSummarizedCell standardCell];
}
if(datapickup.isexpanded==YES)
{
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 44)];
av.opaque = NO;
av.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"dashboardCellEffect.png"]];
cell.backgroundView = av; //tried this
// [cell setBackgroundView:av]; //and this also... but still image is set in the box and not the whole cell
}
else{
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 44)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
cell.backgroundView = av;
}
return cell;
}
Try this code. it worked for me.
UIImageView * ac= [[UIImageView alloc] init];
ac.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"image.png"]];
cell.backgroundView =ac;
cell.backgroundColor = [UIColor clearColor];
try this?
UIImage *backgroundImage = [UIImage imageNamed:#"dashboardCellEffect.png"];
UIView *backgroundCellView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 44)];
// redraw the image to fit cell's size
UIGraphicsBeginImageContextWithOptions(backgroundCellView.frame.size, NO, 0.f);
[backgroundImage drawInRect:CGRectMake(0.f, 0.f, backgroundCellView.frame.size.width, backgroundCellView.frame.size.height)];
UIImage *refinedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[backgroundCellView setBackgroundColor:[UIColor colorWithPatternImage:refinedImage]];
cell.backgroundView = backgroundCellView;
you are using imageview so it shows color in only imageview instead of whole cell
you can try below method
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(datapickup.isexpanded==YES)
{
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"dashboardCellEffect.png"]];
}
else {
cell.backgroundColor = [UIColor clearColor];
}
}
tyr above code your problem will solve.