I would like to know if how can i build a dynamic UItable with random background image? e.g i have 6 images called back1.png, back2.png, back3.png......back6.png.
What code to be added in the "cellForRowAtIndexPath"
I only can sequentially assign each cells with a specific image as follow,
if(indexPath.row == 0){
newView.backgroundColor = [UIColor [UIColor colorWithPatternImage:[UIImage imageNamed:#"lightwood1.jpg"]];
}
if(indexPath.row == 1){
newView.backgroundColor = [UIColor [UIColor colorWithPatternImage:[UIImage imageNamed:#"lightwood2.jpg"]];
}
if(indexPath.row == 2){
newView.backgroundColor = [UIColor [UIColor colorWithPatternImage:[UIImage imageNamed:#"lightwood3.jpg"]];
}
But how I can just change it to randomly with unlimited rows in a UITableView?
try this...
int lowerBound = 1;
int upperBound = 7;
int randomValue = lowerBound + arc4random() % (upperBound - lowerBound);
newView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:[NSString stringWithFormat:#"lightwood%d.jpg", randomValue]]];
Try This one, May be help full
newView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[NSString stringWithFormat:#"lightwood%d.jpg", indexPath.row]]];
indexPath.row increase one by one and your images name also increasing one by one....
Thanks
Related
I am trying to add a background image to UITableViewCell using the following code:
UIImage *bgImage=[UIImage imageNamed:#"green-cell-row.png"];
UIImageView *bgForFirst=[[UIImageView alloc] initWithImage:bgImage];
cell.backgroundView=bgForFirst;
[cell.contentView addSubview:venueDisplayImage];
[cell.contentView addSubview:venueDisplayName1];
[cell.contentView addSubview:venueDisplayName2];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
However, I get venueDisplayName on white space around it. What would be the best way to fix this and correctly add my background image?
Try this way:
1) TabelView set the background like:
tblView.backgroundView.alpha = 0.0;
tblView.backgroundColor=[UIColor clearColor];
2) Add the image view in cellForRowAtIndexPath og delegate method of tableview:
UIImageView *bckView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 300,80)];
cell.backgroundView.alpha = 0.0;
cell.backgroundColor=[UIColor clearColor];
bckView.image = [UIImage imageNamed:#"cell.png"];
bckView.contentMode = UIViewContentModeScaleToFill;
[cell.contentView addSubview:bckView];
Did you try to change backgroundColor or venueDisplayName to [UIColor clearColor] or to [UIColor greenColor]?
For performance reasons try to avoid using non-opaque views in UITableViewCell.
You have to use the property of cell
there are 2 property you can use
1) cell.backgroundColor
2) cell.backgroundView
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageName:#""]];
In second case you have to set any view as a background view
In my UITableViewCell subclass, I'm overriding the setHighlighted and setSelected methods to change the look of the cell when it's selected, but whenever I set the accessoryView property in either of the methods, all my other code that changes the font and shadow colors is ignored entirely.
For example, using this code will change the text color of the text and detail labels
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
if (selected) {
self.textLabel.textColor = [UIColor whiteColor];
self.textLabel.shadowColor = [UIColor clearColor];
self.detailTextLabel.textColor = [UIColor whiteColor];
self.detailTextLabel.shadowColor = [UIColor clearColor];
}
}
But the moment I add the custom accessoryView to the mix, all the other code is ignored, however the accessoryView image does appear.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
if (selected) {
self.textLabel.textColor = [UIColor whiteColor];
self.textLabel.shadowColor = [UIColor clearColor];
self.detailTextLabel.textColor = [UIColor whiteColor];
self.detailTextLabel.shadowColor = [UIColor clearColor];
self.accessoryView =
[[UIImageView alloc] initWithImage:styleImage(#"/icons/disclosure_selected.png")];
}
}
Is there something I'm doing wrong? How can I properly customize the accessoryView and the rest of the cell's code during the selected and highlighted states?
I ended up just creating my own UIImageView and hiding the built in one.
I think accessoryView is a UIButton not a UIImageView...
Try this :
accessory = [UIButton buttonWithType:UIButtonTypeCustom];
[accessory setImage:[UIImage imageNamed:#"/icons/disclosure_selected.png"] forState:UIControlStateNormal];
accessory.frame = CGRectMake(0, 0, 26, 26); //You can change it
accessory.userInteractionEnabled = YES; //You can change it too
self.accessoryView = accessory;
Hope it'll help
I was trying to use a background image for my TableView so I don't get the leftover tableview cells when there isn't enough data to populate the whole table on screen, so I tried to follow this URL, but much simpler: http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
I do this in the viewcontroller that has an outlet to my UITableView:
UIImage *backgroundTableImage = [UIImage imageNamed:#"form_background.png"]; // basically a gray->white gradient
UIImageView *tableBackgroundImageView = [[UIImageView alloc] initWithImage:backgroundTableImage];
tableBackgroundImageView.frame = self.TableView.bounds;
self.TableView.backgroundView = tableBackgroundImageView;
[tableBackgroundImageView release];
self.backgroundColor = [UIColor clearColor];
Then I have a custom subclass of UITableViewCell that I create in IB. In IB, it's background is set to White-default. My cells are all transparent, and I only see my background. How do I get my cells to stay white.
At first take a view in your .h file UIView *newView; declare its property and synhesize it at your .m file
Now in cellForRowAtIndexPath
if(cell==nil){
tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"imagename.png"]];
................
.........
newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
[cell.contentView addSubview:newView];
................
...............
}
if(indexPath.row == 0){
newView.backgroundColor = [UIColor blueColor];
}
if(indexPath.row == 1){
newView.backgroundColor = [UIColor redColor];
}
if(indexPath.row == 2){
newView.backgroundColor = [UIColor greenColor];
}
If any further question please knock me.
Edit: It works fine, But still when the table get Loads Some gap remains between table top and table header abt 30 pxcls(or 1 row) ..?
Please help....
I am scrolling it works fine. All the table rows are scrolling and table header is fixed.
But when I scroll to end of the table it looses its property of fix. I mean is boundry scrolling. I want to freeze it absolutely. At the boundary condition it looses the top position of the table and slides about 20 pxcls.
//Header Format starting
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 20.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (tableView.tableHeaderView) { // header was already created... go away
return tableView.tableHeaderView;
}
CGFloat width = 300.0f;
CGRect rectArea = CGRectMake(10.0f, 5.0f, width, 25.0);
tableView.tableHeaderView = [[[UIView alloc] initWithFrame:rectArea] autorelease];
//UIColor *orange = [UIColor colorWithRed:(255.0f/255.0f) green:(228.0f/255.0f) blue:0.0f alpha:1.0f];
[tableView.tableHeaderView setBackgroundColor:[UIColor grayColor]];
rectArea = CGRectMake(02.0f, 1.0f, width, 20.0);
UILabel *lbl = [[UILabel alloc] initWithFrame:rectArea];
lbl.text = NSLocalizedString(#"Bill Total", #"");
lbl.textAlignment = UITextAlignmentLeft;
//lbl.font = [UIFont systemFontOfSize:13.0f];
lbl.font = [UIFont fontWithName:#"Courier New" size:14];
lbl.font=[UIFont italicSystemFontOfSize:14];
lbl.textColor = [UIColor blackColor];
lbl.backgroundColor = [UIColor clearColor];
lbl.numberOfLines = 2.0f;
lbl.lineBreakMode = UILineBreakModeWordWrap;
//[lbl sizeToFit];
[tableView.tableHeaderView addSubview:lbl];
[lbl release];
// self.table.tableHeaderView.layer.cornerRadius = 6.0f;
return table.tableHeaderView;
}
If I understand your question correctly, it sounds like you just need the table not to bounce. If that's the case, all you need to do is set yourTable.bounces = NO; in your viewDidLoad function. Either that or uncheck the "Bounces" option in the NIB if you used one to layout your table.
Here's how to fix the problem you mentioned in your edit... You need to replace the following references to tableView.headerView with references to a new view that's not being passed into the delegate method.
UIView *headerView = [[[UIView alloc] initWithFrame:rectArea] autorelease];
//...
[headerView setBackgroundColor:[UIColor grayColor]];
//...
[headerView addSubview:lbl];
//...
return headerView;
I tried it and it did the trick for me. Hope that helps.
I'm working on a UITableView whose cells contain an UIImageView subclass which gets data from a URL and cache images to the iphone disk.
Problem is, event with cached images the scrolling tends to be stuttering. So I searched a bit and found ABTableViewCell ( github.com/enormego/ABTableViewCell ) which is supposed to dramatically improve scrolling smoothness.
But, even with the example provided ( blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview ) I don't really get what I am supposed to do.
I tried to do this: I created a class which inherits ABTableViewCell, added some UILabels and the UIImageView as class properties, and implemented methods this way: allocate and initialize subviews (labels, image) in the initialize class method, storing them in static pointers, and then set class properties in - (void)drawContentView:(CGRect)r highlighted:(BOOL)highlighted along with background color setting shown in example. Here's the result:
static AsyncUIImageView* image = nil; // A subclass using ASIHTTPRequest for image loading
static UILabel* label1 = nil;
static UILabel* label2 = nil;
+ (void)initialize {
if (self == [ResultTableViewCell class]) {
image = [[[AsyncUIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 60)] retain];
label1 = [[[UILabel alloc] initWithFrame:CGRectMake(90, 5, 150, 30)] retain];
label1.font = [UIFont fontWithName:#"Helvetica-Bold" size:17];
label1.textColor = [UIColor purpleColor];
label1.backgroundColor = [UIColor clearColor];
label2 = [[[UILabel alloc] initWithFrame:CGRectMake(180, 8, 100, 25)] retain];
label2.font = [UIFont fontWithName:#"Helvetica-Bold" size:12.0];
label2.textColor = [UIColor grayColor];
label2.backgroundColor = [UIColor clearColor];
}
}
- (void)drawContentView:(CGRect)r highlighted:(BOOL)highlighted {
if (self.imageView == nil) {
self.imageView = image;
[self addSubview:image];
self.firstLabel = label1;
[self addSubview:label1];
self.secondLabel = label2;
[self addSubview:label2];
}
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *backgroundColor = [UIColor whiteColor];
UIColor *textColor = [UIColor blackColor];
if (self.selected || self.highlighted) {
backgroundColor = [UIColor clearColor];
textColor = [UIColor whiteColor];
}
[backgroundColor set];
[textColor set];
CGContextFillRect(context, r);
}
This gives me completely black cells, sometimes one has text and image set with correct colors, but its content changes as I scroll down.
Obviously I did not understand what I am supposed to do in drawContentView.
Could someone explain its purpose?
The whole idea is to not add subviews, but to draw the text instead.
Eg.
- (void)drawContentView:(CGRect)r highlighted:(BOOL)highlighted {
[someText drawInRect:r withFont:aFont];
}