How to add UIActivityIndicatorView to UITableViewCell? - iphone

I want to add an UIActivityIndicator to an UITableViewCell. How can I do?

try like this
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"NoReuse"] autorelease];
cell.textLabel.text = #"Loading...";
UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
// Spacer is a 1x1 transparent png
UIImage *spacer = [UIImage imageNamed:#"spacer"];
UIGraphicsBeginImageContext(spinner.frame.size);
[spacer drawInRect:CGRectMake(0,0,spinner.frame.size.width,spinner.frame.size.height)];
UIImage* resizedSpacer = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.image = resizedSpacer;
[cell.imageView addSubview:spinner];
[spinner startAnimating];
return cell;

UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
spinner.frame = CGRectMake(205.0f, 0.0f, 44.0f, 44.0f);
spinner.tag = 3;
[cell.contentView addSubview:spinner];

Related

How do I show exactly the same color in UIToolbar and NavigationBar?

I'm trying to add a UIToolbar that shows the user karma in a NavigationBar. The problem I have is that the colors are not exactly equals, as you can see at the photo.
So, any idea How could I resolve this? Here is the code:
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 80.0f, 45.00f)];
UILabel *karma = [[UILabel alloc] initWithFrame:CGRectMake(30.0f, 0.0f, 40.0f, 44.00f)];
karma.text = [[NSUserDefaults standardUserDefaults] stringForKey:#"karma"];
karma.font = [UIFont fontWithName:#"Helvetica" size:(14.0)];
karma.textColor = [UIColor colorWithRed:(255.0) green:(255.0) blue:(255.0) alpha:1];
karma.backgroundColor = [UIColor colorWithRed:(64/255.0) green:(64/255.0) blue:(64/255.0) alpha:0];
UIImage *image = [UIImage imageNamed:#"ic_people.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0.0f, 7.5f, 30.0f, 30.00f);
// Add Pin button.
[tools setBarStyle: UIBarStyleDefault];
[tools addSubview:karma];
[tools addSubview:imageView];
// Add toolbar to nav bar.
UIBarButtonItem *karmaNav = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = karmaNav;
...
Thank you.
Why not embed your karma label and image inside of a UIView instead of a UIToolbar. Then, you'd be able to set the background color of that UIView to clearColor.
Something like this:
UIView *tools = [[UIView alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 80.0f, 45.00f)];
tools.backgroundColor = [UIColor clearColor];
UILabel *karma = [[UILabel alloc] initWithFrame:CGRectMake(30.0f, 0.0f, 40.0f, 44.00f)];
karma.text = [[NSUserDefaults standardUserDefaults] stringForKey:#"karma"];
karma.font = [UIFont fontWithName:#"Helvetica" size:(14.0)];
karma.textColor = [UIColor colorWithRed:(255.0) green:(255.0) blue:(255.0) alpha:1];
karma.backgroundColor = [UIColor colorWithRed:(64/255.0) green:(64/255.0) blue:(64/255.0) alpha:0];
UIImage *image = [UIImage imageNamed:#"ic_people.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0.0f, 7.5f, 30.0f, 30.00f);
[tools addSubview:karma];
[tools addSubview:imageView];
UIBarButtonItem *karmaNav = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = karmaNav;

How to set Background image in UITableView cell?

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.

uitableview questions

i try to this condition but don't work. If the senderlabel = key#"sender" use the green.png or the grey.png for every rows.
if (senderLabel.text = [tempMsg objectForKey:#"sender"])
{
[cell.msgText setText:[tempMsg objectForKey:#"message"]];
[messTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//[cell setFrame:CGRectMake(0,0, size.width, size.height)];
[cell.msgText setFrame:CGRectMake(15,3, size.width, result)];
UIImage* balloon = [[UIImage imageNamed:#"grey.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
UIImageView *newImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, size.width+10, result+10)];
UIView *newView =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)];
[newImage setImage:balloon];
[newView addSubview:newImage];
[cell setBackgroundView:newView];
}else {
[cell.msgText setText:[tempMsg objectForKey:#"message"]];
[messTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//[cell setFrame:CGRectMake(0,0, size.width, size.height)];
[cell.msgText setFrame:CGRectMake(15,3, size.width, result)]; //propriété du texte
UIImage* balloon = [[UIImage imageNamed:#"green.png"] stretchableImageWithLeftCapWidth:24 topCapHeight:15];
UIImageView *newImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, size.width+10, result+10)];
UIView *newView =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)];
[newImage setImage:balloon];
[newView addSubview:newImage];
[cell setBackgroundView:newView];
}
return cell;
}
if (senderLabel.text = [tempMsg objectForKey:#"sender"]) is not correct. You need to use isEqual in your comparison.
if ([senderLabel.text isEqual:[tempMsg objectForKey:#"sender"]])
First of all, here
if (senderLabel.text = [tempMsg objectForKey:#"sender"])
you are making an assignment, not a comparison, comparing would be
if (senderLabel.text == [tempMsg objectForKey:#"sender"])
However, that would compare if it's the exact same object in memory, which is probably not what you want, if you want to check if the string has the same text, you have to use
[string isEqualToString:anotherString]
To get indexpath you can use like this:
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:0 inSection:0];
and for comparison:
if ([string compare:anotherString] == NSOrderedSame)

UITableViewCell Set Selected Image

trying to figure out how to set the selected image for for a tableViewCell.
The old way of writing this was cell.selectedImage but that has been deprecated since 3.0.
I've tried numerous things, but can't get it too work.
Thanks!
Josh
According to the Deprecated UITableViewCell Methods doc, you should use the imageView's highlightedImage property as a replacement for selectedImage. For example:
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.imageView.image = [UIImage imageNamed:#"deselected_image.png"];
cell.imageView.highlightedImage = [UIImage imageNamed:#"selected_image.png"];
You can set selected backgroundView like below....
UIImageView *selBGView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"urimage.png"]];
cell.selectedBackgroundView = selBGView;
That didn't worked because you might set the normal state image like this
cell.imageView.image = ///
Try it - It worked for me perfectly!!
UIImageView *selBGView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"urimage_selected.png"]];
cell.selectedBackgroundView = selBGView;
[selBGView release];
UIImageView *BGView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"urimage.png"]];
cell.backgroundView = BGView;
[BGView release];
Try this ...
UIImage *bgImage = [UIImage imageNamed:#"icon_call.png"];
UIImageView *bgImageView = [[UIImageView alloc] initWithImage:bgImage];
[bgImageView setFrame:CGRectMake(280, 2, 30, 38)];
//Finally give this imageView to the cell
[cell.contentView addSubview:bgImageView];
Hope it will solve your problem !
Use this one!:
selectionBackground = [UIImage imageNamed:#"background.png"];
cell.selectedBackgroundView =[[UIImageView alloc] init];
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;

IPhone SDK: Adding a UIActivityIndicatorView to a UITableViewCell

Why doesn't the cell show anything in this code:
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[cell.imageView addSubview:spinner];
[spinner startAnimating];
cell.textLabel.text=#"Carregando...";
[spinner release];
I'm doing this inside tableView:cellForRowAtIndexPath:.
I tried size to fit, create a frame to cell.imageView and a same size frame to the spinner, but nothing works.
What´s wrong with this code?
Thank you..!
A slight modification of the method from Thiago:
I think adding the spinner directly to the cell view felt a little hacky, so I used a 1x1 transparent png as the image view and resized it to be whatever my spinner size is:
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"NoReuse"] autorelease];
cell.textLabel.text = #"Loading...";
UIActivityIndicatorView *spinner = [[[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
// Spacer is a 1x1 transparent png
UIImage *spacer = [UIImage imageNamed:#"spacer"];
UIGraphicsBeginImageContext(spinner.frame.size);
[spacer drawInRect:CGRectMake(0,0,spinner.frame.size.width,spinner.frame.size.height)];
UIImage* resizedSpacer = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.image = resizedSpacer;
[cell.imageView addSubview:spinner];
[spinner startAnimating];
return cell;
That gives a cell that looks like this:
I found the answer...
David Maymudes was partialy right... It´s necessary to have a "background" to the cell.imageView... But must be a image, not just a frame. So just create a UIImage as a "white background" and set in cell.imageView.image. The code will be:
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
UIImage *whiteback = [[UIImage alloc] initWithContentsOfFile:#"whiteback.png"];
cell.imageView.image = whiteback;
[cell.imageView addSubview:spinner];
[spinner startAnimating];
cell.textLabel.text=#"Carregando...";
[whiteback release];
[spinner release];
The whiteback.png is just a 25x25 pixels white square...
Thanks for everyone help... See you...
I think the cell's imageView will probably have a zero-size rectangle, because you haven't put a picture in it. So the spinner is inside but invisible.
So instead of putting the spinner within the imageView, just put it within the cell...
[cell addSubview:spinner];
spinner.frame = CGRectMake(145, 0, 30, 30); // if you want it centered horizontally...
you could also do
cell.accessoryView = spinner;
to put the spinner over at the far right of the cell.