I tried to set a background image to a label in UITableViewCell,But while running ,only the background image is displayed in label,text is not displayed.
This is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"TableCell";
// Dequeue or create a cell of the appropriate type.
TableCell *cell = (TableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
UIImageView *preparationLabelBg = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"cellbg.jpeg"]];
[cell.preparationLabel addSubview:preparationLabelBg];
cell.preparationLabel.backgroundColor=[UIColor clearColor];
[cell.preparationLabel sendSubviewToBack:preparationLabelBg];
[cell.preparationLabel setOpaque:NO];
}
[cell.preparationLabel setFont:[UIFont fontWithName:#"Marker Felt" size:14]];
// Configure the cell.
if (btnselected==1)
{
cell.preparationLabel.text = [recipe.ingredients objectAtIndex:indexPath.row];
}
else
{
cell.preparationLabel.text = [recipe.preparation objectAtIndex:indexPath.row];
}
return cell;
}
Why text is not displayed here?Can anyone help?
Try this code,
theLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"cellbg.jpeg"]];
You should not add background image to UILable instead add background image to tableCell as subnview or add background image as [cell.contentview addSubview:[UIImage imageNamed:#"cellbg.jpeg"]]
Also use PNG instead of JPEG/JPG. Apple prefer PNG.
Instead of putting background to the lable put one ImageView at the back of lable and then set the image to that ImageView. It will work.
Related
I use below code to try to make tableview cell background clear colour when selected and it works without checkbox but when there is it's not clear colour totally.
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
cell.tintColor = [UIColor redColor];
UIView *selectionView = [UIView new];
selectionView.backgroundColor = [UIColor clearColor];
[[UITableViewCell appearance] setSelectedBackgroundView:selectionView];
As you can see from below picture, it's only clear under checkbox but not entire cell. But when there's no checkbox (not editing mode) it's entirely clear. How can I fix this? Thanks in advance.
If I remove the above code, the result will look like this. The white background will cover entire cell. I don't know why the above code can just set clear background to the area beneath checkbox?
add this method
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
}
Need to set clear background of cell and cell'contentView.
how about setting the selection style to none instead of blue?
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
I have find solution, We can edit below code
- (void)viewDidLoad {
[super viewDidLoad];
//Add below to line
[self.tblContact setAllowsMultipleSelectionDuringEditing:YES];
[self.tblContact setEditing:YES animated:YES];
//tblContact is tableView Outlet
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//ContactTableViewCell is xib file of UITableViewCell
static NSString *identifier = #"ContactTableViewCell";
ContactTableViewCell *cell = (ContactTableViewCell *)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ContactTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
cell.tintColor = [UIColor redColor];
UIView *selectionView = [UIView new];
selectionView.backgroundColor = [UIColor clearColor];
[[UITableViewCell appearance] setSelectedBackgroundView:selectionView];
cell.backgroundColor = [UIColor clearColor];
return cell;
}
Normally in iOS, the cells in a tableview are loading on scrolling. In this time I'm setting a label text. In this case I'm setting this text dynamically. When I'm scrolling down it's working as I expected. But when It's scrolling up, it's not working as expected. Here what I tried.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"moduleCell";
DateTableViewCell *cell = (DateTableViewCell*)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"DateTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#""]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (self.newsSegmentControl.selectedSegmentIndex == 1) {
[cell.dateView setHidden:NO];
}else{
[cell.dateView setHidden:YES];
}
NSString *day = [self getDayLabel:date];
if ([dateReturned isEqualToString:day]) {
[cell.lblDate setText:#""];
if (self.newsSegmentControl.selectedSegmentIndex == 1) {
[cell.dateView setHidden:YES];
}
}else{
[cell.lblDate setText:day];
dateReturned = day;
}
}
How may I avoid re-draw the label text in cell on scrolling?
I have an UITextView and It contains 2 links. I set the font color using,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"moduleCell";
CustomerTableViewCell *cell = (CustomerTableViewCell*)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomerTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#""]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell setSeparatorInset:UIEdgeInsetsZero];
cell.appContext = self.appContext;
currentCustomer = [customersArr objectAtIndex:indexPath.row-1];
[cell setCellDetails:customersArr WithIndex:row-1 withParentView:self];
cell.postedText.linkTextAttributes= #{NSBackgroundColorAttributeName: [UIColor whiteColor], NSForegroundColorAttributeName: [UIColor blackColor]};
}
return cell;
}
This change the font color. Then I tried to change the background color of it using NSBackgroundColorAttributeName But it doesn't change the background color of the link.
I need to change the link like,
Can anyone help me on this?
I have a UITableView with a Custom Cell. Some custom cells are being loaded with an image.
And I'm adding a custom separator for the cells that have an image. The problem arises on scrolling. Whenever I scroll, the UIView opacity changes.
Here is my code :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
if (cell && (NSNull*) imageString != [NSNull null])
{
UIView* separatorLineView; = [[UIView alloc] initWithFrame:CGRectMake(0, -1, 320, 5)];
separatorLineView.backgroundColor = [UIColor blackColor];
separatorLineView.alpha = 0.2;
[cell.contentView addSubview:separatorLineView];
}
Alpha for the view given is 0.2 and on scrolling it becomes thicker and thicker. How can I solve this issue?
Do what #rmaddy and #rdelmar have suggested but since you're using a custom cell anyways, the quickest way, imho, would be:
Add this line to the cell's contentView via the Interface Builder directly
#property (strong, nonatomic) IBOutlet UIView *vwLine;
also #synthesize vwLine;
Set it's frame, background color & alpha value (obviously)
Set it's hidden property to YES
When you do the imageString != [NSNull null] check
if TRUE then [cell.vwLine setHidden:NO];
Example:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell"
owner:self
options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
if (imageString != [NSNull null]) {
[cell.vwLine setHidden:NO];
}
}
I think the problem is that you're adding a separatorLineView to cells that already have one as you scroll, so what you're seeing is lines on top of other lines. You could fix that by adding a tag to your line view, and checking if the cell has a subview with that tag, and if so, don't add another line.
In your code might be problem of reusability of cell. So try with following code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
.
.
.
.
if(cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
if(cellHasImage == YES)
{
//// just initialize UIView and add as addSubview of cell.contentView
UIView* separatorLineView; = [[UIView alloc] init];
separatorLineView.frame = CGRectMake(0, -1, 320, 5);
separatorLineView.tag = 111; // set whatever you like
[cell.contentView addSubview:separatorLineView];
}
}
.
.
.
/// get UIView base on it's tag
if(cellHasImage == YES)
{
UIView* separatorLineView; = (UIView *) [cell.contentView viewWithTag:111];
separatorLineView.backgroundColor = [UIColor blackColor];
separatorLineView.alpha = 0.2;
}
.
.
.
return cell;
}
In cellForRowAtIndexPath I am adding a UIImageView to cell.contentView. The problem is that when the cell scrolls off the screen and back on, it adds the same image again on top of the one that is already there. This happens continuously until I get a very stacked-up blurry image.
Do you have to keep removing any image views that you add to cell.contentView? If so, in what delegate method do you do that in?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
MyTableCell *cell = (MyTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.jpg"]];
imageView.center = CGPointMake(310, 48);
[cell.contentView addSubview:imageView];
[imageView release];
return cell;
}
If you don't want to keep putting imageViews in your cell, you have to do all the customization inside the if(cell==nil) block, otherwise it will add one every time a cell recycles. When using cell recycling you always want to keep anything that is consistent for all of your cells in that block so that they only get added once.
Ex:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
MyTableCell *cell = (MyTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
//Add custom objects to the cell in here!
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.jpg"]];
imageView.center = CGPointMake(310, 48);
[cell.contentView addSubview:imageView];
[imageView release];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
If each cell should need a different image, you can try something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
static const int ImageViewTag = 1234; //any integer constant
MyTableCell *cell = (MyTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIImageView *imageView;
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
//Add custom objects to the cell in here!
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, imgWidth, imgHeight)];
imageView.center = CGPointMake(310, 48);
imageView.tag = ImageViewTag;
[cell.contentView addSubview:imageView];
[imageView release];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
imageView = [cell viewWithTag:ImageViewTag];
}
imageView.image = yourUIImageForThisCell;
return cell;
}