I have Created custom cell.And i have table view with Grouped style.Now i want to put Backgroung image to each cell of different section.How to set the background image of custom cell.
Here is the code for creating the custom cell.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 305, 90)];
// view.backgroundColor=[UIColor darkGrayColor];
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(11, 9, 61, 55)];
// NSLog(#"imageArray%#",[play.imageArray count]);
NSString *img=[play.imageArray objectAtIndex:indexPath.row];
NSLog(#"img=%#",img);
imageV.image = [UIImage imageNamed:img];
[view addSubview:imageV];
[imageV release];
UILabel *lblAchievementName = [[UILabel alloc] initWithFrame:CGRectMake(90, 10, 168, 21)];
lblAchievementName.text = [play.listData objectAtIndex:indexPath.row];
lblAchievementName.backgroundColor=[UIColor clearColor];
lblAchievementName.textColor=[UIColor orangeColor];
[lblAchievementName setFont:[UIFont fontWithName:#"Arial Black" size:16.5]];
[view addSubview:lblAchievementName];
[lblAchievementName release]
[cell.contentView addSubview:view];
return cell;
Now how to set the backgroung image for this custom view?
You can try this code that will help you.
Put this code in cellForRowAtIndexPath method
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 277, 58)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
av.image = [UIImage imageNamed:#"categorytab1.png"];
cell.backgroundView = av;
set thru setBackgroundView method.Form the ImageView with ur need Image aand set that as,
[cell setBackgroundView:urBgImgView];
You can use following code for different image in different section.
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(11, 9, 61, 55)];
// NSLog(#"imageArray%#",[play.imageArray count]);
NSString *img=[play.imageArray objectAtIndex:indexPath.row];
NSLog(#"img=%#",img);
if (indexPath.section == 0) {
imageV.image = [UIImage imageNamed:img0];
}
else if (indexPath.section == 1) {
imageV.image = [UIImage imageNamed:img1];
}
else if (indexPath.section == 2) {
imageV.image = [UIImage imageNamed:img2];
}
[view addSubview:imageV];
[imageV release];
i'd rather to add a CALayer or UIImageView to be the backgroud view. Because i used to set the BackgroundColor, but it never workd for me. so i changed my mind by this way.
Related
Every time you click on the create button 4 rows of images are created. I'm using this technique for the game mastermind. When you click on the clear button every imageview has to be set to nil, but now only the last created 4 imageviews are set to nil. Any help would be appreciated!
- (IBAction)create {
_imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(134, y, 30, 30)];
_imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(170, y, 30, 30)];
_imgView3 = [[UIImageView alloc] initWithFrame:CGRectMake(206, y, 30, 30)];
_imgView4 = [[UIImageView alloc] initWithFrame:CGRectMake(241, y, 30, 30)];
_imgView1.image = [UIImage imageNamed:[textfield_code objectAtIndex:0]];
_imgView2.image = [UIImage imageNamed:[textfield_code objectAtIndex:1]];
_imgView3.image = [UIImage imageNamed:[textfield_code objectAtIndex:2]];
_imgView4.image = [UIImage imageNamed:[textfield_code objectAtIndex:3]];
[self.view addSubview:_imgView1];
[self.view addSubview:_imgView2];
[self.view addSubview:_imgView3];
[self.view addSubview:_imgView4];
y += 41;
}
- (IBAction)clear {
[_imgView1 setImage:nil];
[_imgView2 setImage:nil];
[_imgView3 setImage:nil];
[_imgView4 setImage:nil];
}
enumerate the view's subviews
for(UIImageView *v in self.view.subviews) {
if([v isKindOfClass:[UIImageView class]) {
[v setImage:nil];
}
}
BUT this is 'dangerous' as it removes ANY image view there is. it'd be better to use tags too.
e.g.
define it as #define MyTag 100
and when will creating v.tag = MyTag
and then in the loop check it if(v.tag == MyTag)
https://github.com/nicklockwood/iCarousel/issues/278
I am going to use the iCarousel to show the images linearly.
These images will be gotten dynamically.
Assume that there are 2 images.
But when I update iCarousel, there are also 2 images in background as following image.
While debugging code, I found that viewForItemAtIndex function is called 2 times after I called reloadData function.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
So there are 4 images in iCarousel.
How to prevent to be called 2 times?
Please help me to fix this problem asap.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIImageView *puzzlePhoto = nil;
UIImageView *puzzleBG = nil;
UILabel *label = nil;
if (view == nil)
{
view = [[UIImageView alloc] init];
view.frame = CGRectMake(0, 0, 128, 112);
view.backgroundColor = [UIColor clearColor];
view.layer.doubleSided = NO;
puzzlePhoto = [[UIImageView alloc] initWithFrame:CGRectMake(46, 48, 40, 40)];
//! get friend's photo
FForesight *foresightCurrent = (carousel == self.icGameOnwhoAREus) ? [controller.gameManager.m_arrayStartedForesights objectAtIndex:index] : [controller.gameManager.m_arrayEndedForesights objectAtIndex:index];
UIImage *imgPuzzle = [foresightCurrent getPuzzleImage];
if (imgPuzzle)
puzzlePhoto.image = imgPuzzle;
else
puzzlePhoto.image = [UIImage imageNamed:#"puzzle background_small.png"];
puzzleBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 128, 112)];
if (((foresightCurrent.m_intStage & FForesightStage_First) && !foresightCurrent.m_FCreatedByFriend) ||
((foresightCurrent.m_intStage & FForesightStage_Second) && foresightCurrent.m_FCreatedByFriend))
puzzleBG.image = [UIImage imageNamed:#"multi_game_one_item_bg_me.png"];
else
puzzleBG.image = [UIImage imageNamed:#"multi_game_one_item_bg.png"];
//! get friend name
FUser *userFriend = foresightCurrent.m_userFriend;
label = [[UILabel alloc] initWithFrame:CGRectMake(32, 32, 68, 16)];
label.text = userFriend.m_strName;
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:10];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithRed:205.0f/256 green:55.0f/256 blue:2.0f/255 alpha:1];
[view addSubview:puzzlePhoto];
[view addSubview:puzzleBG];
[view addSubview:label];
}
return view;
}
The problem is almost certainly due to the views being recycled incorrectly. Views are re-used with different indexes, you you should never set index-specific properties in your view creation code. Here is the correct way to set up your item view:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIImageView *puzzlePhoto = nil;
UIImageView *puzzleBG = nil;
UILabel *label = nil;
const NSInteger puzzlePhotoTag = 1;
const NSInteger puzzleBGTag = 2;
const NSInteger labelTag = 3;
if (view == nil)
{
//*************************************************
//do setup that is the same for every item view here
//*************************************************
view = [[UIImageView alloc] init];
view.frame = CGRectMake(0, 0, 128, 112);
view.backgroundColor = [UIColor clearColor];
view.layer.doubleSided = NO;
puzzlePhoto = [[UIImageView alloc] initWithFrame:CGRectMake(46, 48, 40, 40)];
puzzlePhoto.tag = puzzlePhotoTag;
[view addSubview:puzzlePhoto];
puzzleBG = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 128, 112)];
puzzleBG.tag = puzzleBGTag;
[view addSubview:puzzleBG];
label = [[UILabel alloc] initWithFrame:CGRectMake(32, 32, 68, 16)];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:10];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithRed:205.0f/256 green:55.0f/256 blue:2.0f/255 alpha:1];
label.tag = labelTag;
[view addSubview:label];
}
else
{
//get references to subviews
puzzlePhoto = (UIImageView *)[view viewWithTag:puzzlePhotoTag];
puzzleBG = (UIImageView *)[view viewWithTag:puzzleBGTag];
label = (UILabel *)[view viewWithTag:labelTag];
}
//*************************************************
//do setup that is different depending on index here
//*************************************************
//! get friend's photo
FForesight *foresightCurrent = (carousel == self.icGameOnwhoAREus) ? [controller.gameManager.m_arrayStartedForesights objectAtIndex:index] : [controller.gameManager.m_arrayEndedForesights objectAtIndex:index];
UIImage *imgPuzzle = [foresightCurrent getPuzzleImage];
if (imgPuzzle)
puzzlePhoto.image = imgPuzzle;
else
puzzlePhoto.image = [UIImage imageNamed:#"puzzle background_small.png"];
if (((foresightCurrent.m_intStage & FForesightStage_First) && !foresightCurrent.m_FCreatedByFriend) || ((foresightCurrent.m_intStage & FForesightStage_Second) && foresightCurrent.m_FCreatedByFriend))
puzzleBG.image = [UIImage imageNamed:#"multi_game_one_item_bg_me.png"];
else
puzzleBG.image = [UIImage imageNamed:#"multi_game_one_item_bg.png"];
//! get friend name
FUser *userFriend = foresightCurrent.m_userFriend;
label.text = userFriend.m_strName;
return view;
}
As for the scrolling inertia problem, if you're still seeing that after applying this fix, file a separate bug report on the github page.
I solved this problem by performing reloadData on mainThread
[carousel performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:YES];
I am trying to customize my tableView in my IOS app. When my tableView(or rather array) is empty, I want to display a customized label instead of the items in the tableView. The label I am referring to is "label0". But something is terribly wrong, my [label0 setHidden:YES]; or [label0 setHidden:NO]; only works in the first block of the if "method"? In the second block (if else) nothing happens no matter what I try to set the label as (hidden or shown).
What have I missed? I cannot see my own fault?
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
UILabel *label0 = [[[UILabel alloc] initWithFrame:CGRectMake(0, 25, tableView.bounds.size.width - 0, 100)] autorelease];
if ([self.searchResults count] == 0){
headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lista2.png"]];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(5, 3, tableView.bounds.size.width - 5, 18)] autorelease];
label.text = #"Information";
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
[headerView addSubview:label];
label0.text = #"Test test test";
label0.textColor = [UIColor blackColor];
label0.backgroundColor = [UIColor whiteColor];
[tableView addSubview:label0];
[label0 setHidden:NO];
}
else {
headerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"lista2.png"]];
UILabel *label2 = [[[UILabel alloc] initWithFrame:CGRectMake(5, 3, tableView.bounds.size.width - 5, 18)] autorelease];
label2.text = #"Search results";
label2.textColor = [UIColor whiteColor];
label2.backgroundColor = [UIColor clearColor];
[headerView addSubview:label2];
[label0 setHidden:YES];
}
return headerView;
}
EDIT
I have moved the code to viewDidLoad and set the property for the UILabel. This have unfortunately not solved my problem....
UILabel *label0 = [[[UILabel alloc] initWithFrame:CGRectMake(0, 25, tableView.bounds.size.width - 0, 100)] autorelease];
[tableView addSubview:label0];
if ([self.searchResults count] == 0){
label0.text = #"Test test test";
label0.textColor = [UIColor blackColor];
label0.backgroundColor = [UIColor whiteColor];
[label0 setHidden:NO];
}
else {
[label0 setHidden:YES];
}
This is because your label0 is created every time this method is called so in "else" you are referring to totally different object (not the one that you added to tableView when array was empty).
You shouldn't be adding subviews to tableView from this method. Consider using viewDidLoad. That way you will be adding label0 only once. To achieve that add label0 as property of your viewController.
You forgot to add label0 as subview please put this line in the else statement
[tableView addSubview:label0];
also I cannot see any benefit from doing so. I believe you can just hide the table view and show another view that has the label. but nesting views that way is not good when you come back to debug this code after 1 month you will struggle to understand it.
You say in your edit that you set the property for the UILabel (I assume you mean that you have a property called label0?). If this is so, then when you alloc init your label, it should be self.label0 = ..... not UILabel *label0 = .....
I am currently creating my own custom section headers but I have never dont any text editing via code.. and my new method that I am using to populate my custom header is doing some weird things as shown below
I would like to change the text to white and be slightly bolder and also make the white background transparent..
this is the code I am using to do this
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
[headerView setBackgroundColor:[UIColor grayColor]];
// Add the label
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.5, 20, 20)];
// do whatever headerLabel configuration you want here
headerLabel.text = [self tableView:tableView titleForHeaderInSection:section];
[headerView addSubview:headerLabel];
// Return the headerView
return headerView;
}
I have tried this
[headerLabel.backgroundColor:[UIColor clearColor]];
etc but its not working :(
I would like to change the text to white...
UILabel's textColor property is your friend here.
And slightly bolder...
No problem! headerLabel.font = [UIFont boldSystemFontOfSize:mySize];
And make a white transparent background.
Whoa, whoa, that is the worst setter syntax ive ever seen!!! My lord, myLabel.backgroundColor is a getter, change:
[headerLabel.backgroundColor:[UIColor clearColor]];
to:
[headerLabel setBackgroundColor:[UIColor clearColor]];
Lucky for you, using your syntax would have just sent a message to nil, which is defaulting the background color of your label to white.
Use following code...
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *headername = [[UILabel alloc]initWithFrame:CGRectMake(20, 5, 270, 34)];
headername.backgroundColor = [UIColor clearColor];
headername.textColor = [UIColor blackColor];
if(section == 0)
{
headername.text = #"Name that u wish";
}
else
{
headername.text = #"Name that u wish";
}
UIView *headerView = [[UIView alloc] init];
UIImageView *tempimage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 300,34)];
tempimage.image = [UIImage imageNamed:#"whitebackground.png"];
[headerView addSubview:tempimage];
[headerView addSubview:headername];
return headerView;
}
Hope, this will help you...chill
all i have done to get the customized header in table is as following and it is working fine for me
UIView *containerView =
[[[UIView alloc]
initWithFrame:CGRectMake(0, 0, 300, 60)]
autorelease];
UILabel *headerLabel =
[[[UILabel alloc]
initWithFrame:CGRectMake(10, 20, 300, 40)]
autorelease];
headerLabel.text = NSLocalizedString(#"Header for the table", #"");
headerLabel.textColor = [UIColor whiteColor];
headerLabel.shadowColor = [UIColor blackColor];
headerLabel.shadowOffset = CGSizeMake(0, 1);
headerLabel.font = [UIFont boldSystemFontOfSize:22];
headerLabel.backgroundColor = [UIColor clearColor];
[containerView addSubview:headerLabel];
self.tableView.tableHeaderView = containerView;
i just put it in viewDidLoad: method
I have a UITableViewCell that contains a UIScrollView. The UIScrollView contains a different number of UIImageViews for each row. Some rows have 5 images in the scroll view, some rows have 15 images in the scroll view. Since the number of UIImageViews in each row is different, I'm stuck allocating new UIImageViews when each row is drawn! This is obviously having a negative impact on scroll performance of my UITableView. How can I resolve this to improve UITableView scrolling?
Additionally, using Instruments' Time Profiler, I've noticed that the method call [tableView dequeueReusableCellWithIdentifier:MyIdentifier] is abnormally slow. Does that help point to the problem?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"HuddleTableCell";
HuddleListItemTableViewCell *cellToReturn = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cellToReturn == nil) {
[[NSBundle mainBundle] loadNibNamed:#"HuddleListItemTableViewCell" owner:self options:nil];
cellToReturn = cell;
UILabel *badgeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
badgeLabel.font = [UIFont boldSystemFontOfSize:16];
badgeLabel.numberOfLines = 2;
badgeLabel.textAlignment = UITextAlignmentCenter;
badgeLabel.textColor = [UIColor whiteColor];
badgeLabel.backgroundColor = [UIColor clearColor];
[cellToReturn.attendee4Image addSubview:badgeLabel];
cellToReturn.othersOverlayLabel = badgeLabel;
[badgeLabel release];
cellToReturn.heartHolderView.layer.cornerRadius = 5;
cellToReturn.heartHolderView.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:.29 green:.39 blue:.57 alpha:1.0] CGColor],(id)[[UIColor colorWithRed:.19 green:.22 blue:.36 alpha:1.0] CGColor], (id)[[UIColor colorWithRed:.102 green:.122 blue:.17 alpha:1.0] CGColor], nil];
cellToReturn.heartHolderView.colors = [NSArray arrayWithObjects:(id)[UIColorFromRGB(0x4A59A8) CGColor], nil];
cellToReturn.heartHolderView.layer.cornerRadius = 5.0;
UIView *polaroidView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 37, 39)];
polaroidView.layer.shadowColor = [UIColor blackColor].CGColor;
polaroidView.layer.shadowOpacity = 0.3;
polaroidView.layer.shadowRadius = 1;
polaroidView.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);
polaroidView.backgroundColor = [UIColor whiteColor];
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(4, 4, 29, 29)];
[polaroidView addSubview:img];
[cellToReturn.organizerImage addSubview:polaroidView];
cellToReturn.polaroidView = polaroidView;
cellToReturn.polaroidImageView = img;
[img release];
[polaroidView release];
cellToReturn.gradientView.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor],(id)[[UIColor colorWithRed:.96 green:.96 blue:.96 alpha:1.0] CGColor],(id)[[UIColor colorWithRed:.85 green:.85 blue:.85 alpha:1.0] CGColor], nil];
cellToReturn.votingTimerLabel.textColor = UIColorFromRGB(0x616163);
cellToReturn.eventDateLabel.textColor = UIColorFromRGB(0x616163);
cellToReturn.eventNameLabel.textColor = UIColorFromRGB(0x616163);
cellToReturn.numInviteesLabel.textColor = UIColorFromRGB(0x616163);
cellToReturn.numVotesLabel.textColor = UIColorFromRGB(0x616163);
self.cell = nil;
}
NSString *organizerFbId = [[self.huddleList objectAtIndex:indexPath.row]objectForKey:#"userId"];
[cellToReturn.polaroidImageView setImageWithURL:[self.organizerImageUrls objectForKey:organizerFbId] placeholderImage:self.placeholderImage];
[[cellToReturn.imageScrollViewHolder subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
if ([self.avatarScrollViews objectForKey:[[self.huddleList objectAtIndex:indexPath.row]objectForKey:#"id"]]) {
[cellToReturn.imageScrollViewHolder addSubview:[self.avatarScrollViews objectForKey:[[self.huddleList objectAtIndex:indexPath.row]objectForKey:#"id"]]];
} else {
UIScrollView *tempImageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 200, 46)];
tempImageScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
NSArray *fbUids = [[self.huddleList objectAtIndex:indexPath.row] objectForKey:#"facebookInvitees"];
int i=0;
for (i=0;i < [fbUids count];i++) {
UIView *polaroidView = [[[UIView alloc] initWithFrame:CGRectMake((i * 45) + (3 * i), 0, 37, 39)] autorelease];
polaroidView.layer.shadowColor = [UIColor blackColor].CGColor;
polaroidView.layer.shadowOpacity = 0.3;
polaroidView.layer.shadowRadius = 1;
polaroidView.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);
polaroidView.backgroundColor = [UIColor whiteColor];
UIImageView *img = [[[UIImageView alloc] initWithFrame:CGRectMake(4, 4, 29, 29)] autorelease];
[img setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://graph.facebook.com/%#/picture", [fbUids objectAtIndex:i]]] placeholderImage:self.placeholderImage];
[polaroidView addSubview:img];
[tempImageScrollView addSubview:polaroidView];
}
[tempImageScrollView setContentSize:CGSizeMake((i*45) + (3 * i), 46)];
[self.avatarScrollViews setObject:tempImageScrollView forKey:[[self.huddleList objectAtIndex:indexPath.row]objectForKey:#"id"]];
[cellToReturn.imageScrollViewHolder addSubview:tempImageScrollView];
[tempImageScrollView release];
}
return cellToReturn;
}
Even there was a fixed number of images in each cell's scroller, you'd be stuck allocating them as the cells are dequeued and reused. The way to get speed is to take up some more space, by keeping the cells' subviews in an array outside of the table.
Create another array that has the same count as your huddleList array. Fill that array with UIScrollViews containing the imageViews. All that code in your cellForRowAtIndexPath method can move to the initialization method for the new array and get replaced by a few simple lines:
// dequeue or build a cell
// tag the scroll views so you can find them
UIScrollView *oldScrollView = (UIScrollView *)[cell viewWithTag:kCELL_SCROLL_VIEW];
// this might be a reused cell, so remove the scroll view if it has one
if (oldScrollView) [oldScrollView removeFromSuperview];
// now add the one that you setup for this cell
// your init code that used to be in this method sets up the frame, the tag, etc
UIScrollView *currentScrollView = [self.cachedScrollViews objectAtIndex:indexPath.row];
[cell addSubview:currentScrollView];
// that's it
Don't forget to release the cachedScrollViews on memory warning