I am facing lots of problem.. i am new to ios development so any help is appreciated.
Problem 1st
I want to display N number of image-view+label over yellow and blue image In custom cell.
I have to parse that how many Number of Image view will be shown on every row and according to that i have to assign image to every image-view in my custom cell. how can i do that?
I fetch image links from database and store it into array but how can i apply links to particular image-view in row.
Just need hints to build above User Interface in iOS.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifierLeft = #"lefthand";
static NSString *CellIdentifierRight = #"righthand";
if (indexPath.row % 2 == 1) // to get two custom cell vice versa.
{
LeftCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierLeft];
if (LeftCell == nil)
{
LeftCell = [[customCellLeft alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierLeft row:indexPath.row];
LeftCell.scrollView = [[cellScrollViewClass alloc] initWithFrame:CGRectMake(0,14,1024,187)];
}
LeftCell.videoImageView1.tag=[ImageLinkArray objectAtIndex:indexPath.row];
LeftCell.label1.text=[TitleArray objectAtIndex:indexPath.row];
return LeftCell;
}
else
{
RightCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierRight];
if(RightCell == nil)
{
RightCell =[[customCellRight alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierRight];
RightCell.scrollView = [[cellScrollViewClass alloc] initWithFrame:CGRectMake(0,14,1024,187)];
}
RightCell.videoImageView1.tag=[ImageLinkArray objectAtIndex:indexPath.row];
RightCell.label1.text=[TitleArray objectAtIndex:indexPath.row];
return RightCell;
}
Custom cell class code...
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier row:(int)row
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
int indexNumber; // variable to store Array index number
int VideoNumber=3; // imageview in particular cell + Train Engine.
scrollView = [[cellScrollViewClass alloc] initWithFrame:CGRectMake(33,14,958,187)]; // Scrollview
scrollView.scrollEnabled=YES;
scrollView.userInteractionEnabled=YES;
scrollView.showsHorizontalScrollIndicator=NO;
scrollView.contentSize = CGSizeMake((335*VideoNumber),187); // To make ContentSize for item to scroll
for (int i = 0; i < VideoNumber; i++)
{ // for loop to add 2 wagon view and 1 engine view on Left hand side Custom cell.
if(i==0) // to check first item must be train Engine
{
CGRect frame;
frame.origin.x = 0;
frame.origin.y = 18;
frame.size.width=186;
frame.size.height= 162;
UIView *EngineView = [[UIView alloc]initWithFrame:frame];
UIImageView *engineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 6, 186, 162)];
UIImage *Image = [UIImage imageNamed:#"loco_left.png"];
engineImageView.image = Image;
[EngineView addSubview:engineImageView];
[scrollView addSubview:EngineView]; // add 1st imageview that is Train Engine to first of scrollview
}
else
{ // else it must be wagon of train.
indexNumber=row*(VideoNumber-1)+(i-1);// to get Index Number of array to to display Images as per cell number
NSLog(#"at Normal row index number is %d",indexNumber);
CGRect frame;
frame.origin.x = (385*i)-432;
frame.origin.y = 18;
frame.size.width=385;
frame.size.height= 163;
UIView *wagon = [[UIView alloc]initWithFrame:frame];
UIImageView *wagonImage = [[UIImageView alloc]initWithFrame:CGRectMake(230, 6, 385, 163)];
UIImage *Image = [UIImage imageNamed:#"wagon_left.png"];
wagonImage.image = Image;
[wagon addSubview:wagonImage];
UIView *videoviewContainer = [[UIView alloc]initWithFrame:CGRectMake(15, 30, 190 , 200)];
videoImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 190, 100)];
UIImage *bgImage = [UIImage imageNamed:#"video.png"];
videoImageView2.image = bgImage;
videoviewContainer.contentMode = UIViewContentModeLeft; // set Video Image view to left hand side of wagon UIView
videoImageView2.tag=indexNumber;
[videoviewContainer addSubview:videoImageView2];
UITapGestureRecognizer *video = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(VideoItemOne:)];
[videoImageView2 addGestureRecognizer:video];
[videoImageView2 setMultipleTouchEnabled:YES];
[videoImageView2 setUserInteractionEnabled:YES];
UIView *textUiView = [[UIView alloc]initWithFrame:CGRectMake(208,28, 150 , 187)];
textUiView.contentMode = UIViewContentModeRight;
label2 = [[UILabel alloc]initWithFrame:CGRectMake(28,10, 150 , 87)];
label2.text=[TitleArray objectAtIndex:indexNumber];
label2.backgroundColor = [UIColor clearColor];
label2.textColor=[UIColor redColor];
label2.numberOfLines = 4;
[textUiView addSubview:label2];
[wagonImage addSubview:textUiView];
[wagonImage addSubview:videoviewContainer];
[scrollView addSubview:wagon];
}
}
[self.contentView addSubview:scrollView];
}
return self;
}
-(void)populateWithImage:myImage andText:myText
{
[videoImageView2 setImageWithURL:[NSURL URLWithString:myImage]
placeholderImage:[UIImage imageNamed:#"video.png"]];
}
Your custom cell should have the following: (note, you have to use a better naming convention and i am just giving an example)
Background Imageview (bgImage)
Main image View (mainImage)
UI Label (mainLabel)
In the init for this cell, based on the indexpath.row, either assign the left or right train image.
Also set the frames for main image view and the label.
MYCustomCell * cell;
if(indexPath.row %2)
{
cell = [tableView dequeueReusableCellWithIdentifier:"OddCell"];
if (cell == nil)
{
cell = [[MYCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierLeft row:indexPath.row];
}
}
else {
cell = [tableView dequeueReusableCellWithIdentifier:"EvenCell"];
if (cell == nil)
{
cell = [[MYCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierLeft row:indexPath.row];
}
}
[cell populateWithImage:myImage andText:myText]; //myImage and myText are to be gotten from the data source which in your case is ImageLinkArray and TitleArray
return cell;
Related
Trying to place a UILabel containing the date in the lower right hand corner of a cell whether it's on an iPhone or iPad.
Here's what I currently have, what do I need to do to get the actual cell size.width, tried doing cell.bounds.size.width as well, same value whether iPhone or iPad:
float xValue = cell.frame.size.width - 100;
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(xValue, 20.0f, 100, 17.0f)];
[nameLabel setTag:1];
[nameLabel setFont:[UIFont boldSystemFontOfSize:15.0]];
nameLabel.textColor = [UIColor grayColor];
nameLabel.text = #"05-12-13";
// custom views should be added as subviews of the cell's contentView:
[cell.contentView addSubview:nameLabel];
ETA, here is the whole custom cell class, maybe someone can see what's causing the duplication of the label:
#interface VideoTableViewCell : UITableViewCell
+ (VideoTableViewCell *)cellForTableView:(UITableView *)tableView;
+ (CGFloat)heightForVideo:(id<VideoProtocol>)video;
- (void)updateCellForVideo:(id<VideoProtocol>)video;
#end
#define MARGIN 10
#implementation VideoTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.textLabel.font = [UIFont boldSystemFontOfSize:14];
self.detailTextLabel.font = [UIFont systemFontOfSize:13];
self.textLabel.numberOfLines = 2;
self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return self;
}
+ (VideoTableViewCell *)cellForTableView:(UITableView *)tableView {
NSString *identifier = #"TweetCell";
VideoTableViewCell *cell = (VideoTableViewCell *)[tableView
dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[VideoTableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:identifier];
}
return cell;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect cvf = self.contentView.frame;
self.imageView.frame = CGRectMake(0.0,
0.0,
cvf.size.height-1,
cvf.size.height-1);
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
CGRect frame = CGRectMake(cvf.size.height + MARGIN,
self.textLabel.frame.origin.y,
cvf.size.width - cvf.size.height - 2*MARGIN,
self.textLabel.frame.size.height);
self.textLabel.frame = frame;
frame = CGRectMake(cvf.size.height + MARGIN,
self.detailTextLabel.frame.origin.y,
cvf.size.width - cvf.size.height - 2*MARGIN,
self.detailTextLabel.frame.size.height);
self.detailTextLabel.frame = frame;
}
- (void)setDateLabelWithDate:(NSDate *)date {
float xValue = self.frame.size.width - 70;
// create a custom label: x y width height
UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(xValue, 40.0f, 100, 12.0f)];
dateLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin;
[dateLabel setFont:[UIFont boldSystemFontOfSize:13.0]];
dateLabel.textColor = [UIColor grayColor];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yy"];
NSString *dateString = [dateFormatter stringFromDate:date];
dateLabel.text = dateString;
[self.contentView addSubview:dateLabel];
}
+ (CGFloat)heightForVideo:(id<VideoProtocol>)video {
//create a dummy cell
VideoTableViewCell *sampleCell = [[VideoTableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:nil];
[sampleCell updateCellForVideo:video];
//calculate the sizes of the text labels
CGSize textSize = [video.title sizeWithFont: [VideoTableViewCell textLabelFont]
constrainedToSize:sampleCell.textLabel.frame.size
lineBreakMode:UILineBreakModeWordWrap];
CGFloat minHeight = 51 + 10; //image height + margin
return MAX(textSize.height + 20, minHeight);
}
+ (UIFont *)textLabelFont {
return [UIFont systemFontOfSize:13];
}
- (void)updateCellForVideo:(id<VideoProtocol>)video {
// set the text to the date with the tweet text
self.textLabel.text = video.title;
id<VideoAttrProtocol> speaker = [[video.speakers allObjects] objectAtIndex:0];
self.detailTextLabel.text = speaker.name;
[self setDateLabelWithDate:video.post_date];
NSURL *url = [NSURL URLWithString:video.thumbnail];
[self.imageView setImageWithURL:url
placeholderImage:[UIImage imageNamed:#"Logo.png"]];
}
#end
You need to set the label's autoresizingMask. Put the label in the correct place based on the cell's initial size, whatever it is. With the autoresizingMask set properly, the label will adjust as the cell's size changes.
Just before adding the label to the content view, add:
// Keep the label in the bottom-right corner
nameLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
Update based on all of the posted code:
In your custom cell, you create and add a label every time the setDataLabelWithDate: method is called.
You call this method from updateCellForVideo: and possibly from your view controller (you don't show that code).
You need to update your code so you do not add a new label to the cell every time setDataLabelWithDate: is called. Only add the label once.
My code parts in two:
The first is the "cellForRowAtIndexPath" method for my table view. In this table view i'm supposed to show 3 buttons in each cell and each button is a different image. Images are in an NSMutableArray call "photosArray". It looks like this.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (photosArray.count > 0) {
for (int i = 0; i < 3; i++) {
if (photosArray.count > photoCounter) {
//position the UiButtons in the scrollView
CGRect frame = CGRectMake((i*100) + 30, 5, 60, 60);
//Create the UIbuttons
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTag:i];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.frame = frame;
button.layer.borderWidth = 2;
button.layer.cornerRadius = 5;
button.clipsToBounds = YES;
//Add images to the button
NSLog(#"PhotosArray count is: %d",photosArray.count);
if ([[photosArray objectAtIndex:0] isKindOfClass:[UIImage class]]) {
NSLog(#"UIImage class true");
}
if ([[photosArray objectAtIndex:0] isMemberOfClass:[UIImage class]]) {
NSLog(#"UIImage class true");
}
UIImage *btnImg = [self.photosArray objectAtIndex:photoCounter];
//UIImage *btnImg2 = [UIImage imageNamed:#"GameOver"];
photoCounter++;
[button setBackgroundImage:btnImg forState:UIControlStateNormal];
[cell.contentView addSubview:button];
}
}
}else{
UILabel *noImages = [[UILabel alloc] initWithFrame:CGRectMake(0, 25, 320, 20)];
noImages.text = #"No images";
noImages.textAlignment = NSTextAlignmentCenter;
[cell.contentView addSubview:noImages];
}
return cell;
}
The second part is where I load my pictures into "photosArray". I am using WSAssetPicker to load multiple photos from my asset library.
Here is the code:
- (void)assetPickerController:(WSAssetPickerController *)sender didFinishPickingMediaWithAssets:(NSArray *)assets
{
// Dismiss the WSAssetPickerController.
[self dismissViewControllerAnimated:YES completion:^{
arep = [[ALAssetRepresentation alloc] init];
for (ALAsset *asset in assets) {
UIImage *imageA = [[UIImage alloc] initWithCGImage:asset.defaultRepresentation.fullScreenImage];
[self.photosArray addObject:imageA];
}
NSLog(#"%d",photosArray.count);
photoCounter = 0;
[self.tableView reloadData];
}];
}
And now for the issue, the buttons that are created have no image in it. I only get a transparent button with a border and rounded corners.
Why is that?
It was recently brought to my attention that I omitted an important piece from the README (which is now updated).
It is necessary to hold a strong reference of WSAssetPickerController if you are accessing ALAsset objects in the dismissViewControllerAnimated:completion: completion block.
If you do not hold a strong reference to the picker controller before calling dismissViewControllerAnimated:completion: the picker controller will be released before the completion block is executed resulting in the ALAssetsLibrary (used internally in the WSAssetPicker code) from being released before you try to access the ALAssets in the assets array passed in the assetPickerController:didFinishPickingMediaWithAssets: delegate method.
If ALAssetsLibrary gets released, the ALAssets owned by the ALAssetsLibrary will expire and calls to asset.defaultRepresentation.fullScreenImage will return null.
The following will preserve the ALAssetsLibrary while dismissing the picker controller's view:
- (void)assetPickerController:(WSAssetPickerController *)sender didFinishPickingMediaWithAssets:(NSArray *)assets
{
// Hang on to the picker to avoid ALAssetsLibrary from being released.
self.picker = sender;
__block id weakSelf = self;
// Note: Instead of `id` you could specify the class of `self` order to access properties with dot notation.
// Dismiss the WSAssetPickerController.
[self dismissViewControllerAnimated:YES completion:^{
// Do something with the assets here.
// Release the picker.
[weakSelf setPicker:nil];
}];
}
An alternative would be to process the array of ALAssets before calling dismissViewControllerAnimated:completion:, but that could cause the picker controller's dismissal to delay resulting in a poor user experience.
My app's UI requires grids of 9 images that are paginated.
To do this I'm creating several UITables inside a UIScrollView (with a UIPageControl):
My table is coming from a xib file that I register. Each table cell has 3 buttons in it.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
int numPages = ([self.images count] + 8) / 9;
NSLog(#"Page count: %i", numPages);
// Set pages
self.pageControl.numberOfPages = numPages;
for (int i = 0; i < numPages; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [UIColor blackColor];
[self.scrollView addSubview:subview];
// Let's try and draw a table
UITableView *table = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
table.delegate = self;
table.dataSource = self;
table.tag = 100 + i;
table.separatorColor = [UIColor clearColor];
[table setBackgroundColor:[UIColor blackColor]];
[table registerNib:[UINib nibWithNibName:#"Cell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:#"TestCell"];
[self.scrollView addSubview:table];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * numPages, self.scrollView.frame.size.height);
}
My problem is that the view loads slowly (5+ seconds). The images being loaded into the buttons are local and not large.
My cellForRowAtIndexPath looks like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TestCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// What table/page are we looking at?
// Check the tag
int page = tableView.tag - 100;
NSLog(#"We are on page/table: %i", page);
// Configure the cell
// Our cells have 3 button images
// So we'll loop and set each image
for (int i = 0; i < 3; i++) {
// From our images array, the item we want
// Will be the row * 3 + i
int imageIndex = (page * 9) + (indexPath.row * 3) + i;
NSLog(#"Image index is: %i", imageIndex);
// Button tags are 10-12 (or 10 + i)
UIButton *button = (UIButton *)[cell viewWithTag:(10 + i)];
[button addTarget:self action:#selector(poseSelected:) forControlEvents:UIControlEventTouchUpInside];
// We have to make sure we don't go out of the bounds of
// our images array (might not have /3 exactly)
if (imageIndex > [self.images count] - 1) {
// We have an extra button, let's hide it
button.hidden = YES;
} else {
// Get the image and file nameefrom our array
NSDictionary *imageDict = (NSDictionary *)[self.images objectAtIndex:imageIndex];
NSString *fileName = [imageDict objectForKey:#"file"];
// Make the thumb name
NSString *thumbName = [fileName stringByReplacingOccurrencesOfString:#".jpg"
withString:#"-THUMB.jpg"];
// NSLog(#"Thumb name is: %#", thumbName);
// Set the button image
UIImage *btnImage = [UIImage imageNamed:thumbName];
[button setImage:btnImage forState:UIControlStateNormal];
}
}
return cell;
}
When I look at the log output, All of the cells are getting setup right away. Why is this?
Shouldn't table views off the screen not be rendered? Or am I using dequeueReusableCellWithIdentifier the wrong way?
You are correct, you are misinterpreting how cellForRowAtIndexPath and a scroll view works. Your cells are getting loaded immediately because your tableviews are all subviews in the scrollview. So the tableviews as subviews will get loaded immediately when the scrollview is added to the view hierarchy. The cellForRowAtIndexPath does recycling for cells that are disappearing when you scroll a tableview (by design this makes sense). My recommendation would be to use a recycling scrollview, or even better a custom gridview implementation (appears to be what you need). I have have some examples in my library, but it might be worth search for the best grid view or image viewer than meets your needs. My library can be found here:
https://github.com/daltoniam/GPLib-iOS
You are going to want to take a look at:
https://github.com/daltoniam/GPLib-iOS/tree/master/GPLib/Views/ImageViews
https://github.com/daltoniam/GPLib-iOS/tree/master/GPLib/Views/GridView
As I stated above, it might be worth looking for a grid view/image view more along the lines your UI needs then what my lib provides, but should be a good starting point. Any questions let me know.
i want to create an application in one row of tableView there will be 4 images of button which will be loaded from server in background because if i directly load them than the table view will hang some. for this i have used this code for creating button in cell and performing to download images in background.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *hlCellID = #"hlCellID";
UITableViewCell *hlcell = [tableView dequeueReusableCellWithIdentifier:hlCellID];
if(hlcell == nil) {
hlcell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:hlCellID] autorelease];
hlcell.accessoryType = UITableViewCellAccessoryNone;
hlcell.selectionStyle = UITableViewCellSelectionStyleNone;
}
int section = indexPath.section;
NSMutableArray *sectionItems = [sections objectAtIndex:section];
int n = [sectionItems count];
int i = 0, j = 0, tag = 1;
int x = 10;
int y = 30;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (i = 1; i<= n ; i++) //[arr count]
{
for(j=1; j<=4;j++)
{
if (i>=n) break;
Item *item = [sectionItems objectAtIndex:i];
CGRect rect = CGRectMake(x = x, y = y, 68, 65);
UIButton *button=[[UIButton alloc] initWithFrame:rect];
[button setFrame:rect];
UIImage *buttonImageNormal=[UIImage imageNamed:item.image];
[button setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
[button setContentMode:UIViewContentModeCenter];
// set the image to be loaded (using the same one here but could/would be different)
NSURL *imgURL = [NSURL URLWithString:#"http://londonwebdev.com/wp-content/uploads/2010/07/featured_home.png"];
// Create an array with the URL and imageView tag to
// reference the correct imageView in background thread.
NSMutableArray *arr = [[NSArray alloc] initWithObjects:imgURL, [NSString stringWithFormat:#"%d", tag], nil ];
// Start a background thread by calling method to load the image
[self performSelectorInBackground:#selector(loadImageInBackground:) withObject:arr];
// button.tag = [tagValue intValue];
button.tag = tag;
//NSLog(#"....tag....%d", button.tag);
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[hlcell.contentView addSubview:button];
[button release];
tag++;
x = x + 77;
}
x = 10;
y = y + 74;
}
[pool release];
return hlcell;
}
the above code work perfectly but when i download the images and trying to assign at the some particular button than i can't find the button tags althoug i can find the button tags while touchupinside action.
- (void) loadImageInBackground:(NSArray *)urlAndTagReference {
NSLog(#"Received URL for tagID: %#", urlAndTagReference);
// Create a pool
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Retrieve the remote image. Retrieve the imgURL from the passed in array
NSData *imgData = [NSData dataWithContentsOfURL:[urlAndTagReference objectAtIndex:0]];
UIImage *img = [[UIImage alloc] initWithData:imgData];
// Create an array with the URL and imageView tag to
// reference the correct imageView in background thread.
NSMutableArray *arr = [[NSArray alloc] initWithObjects:img, [urlAndTagReference objectAtIndex:1], nil ];
// Image retrieved, call main thread method to update image, passing it the downloaded UIImage
[self performSelectorOnMainThread:#selector(assignImageToImageView:) withObject:arr waitUntilDone:YES];
[pool release];
}
- (void) assignImageToImageView:(NSArray *)imgAndTagReference
{
// Create a pool
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int i;
UIButton *checkView;
// [imagesForCategories addObject:[imgAndTagReference objectAtIndex:1]];
// UITableViewCell *cell = [celebCategoryTableView cellForRowAtIndexPath:[imgAndTagReference objectAtIndex:1]];
// UIImageView *profilePic = (UIImageView *)[cell.contentView viewWithTag:20];
// profilePic.image = [imgAndTagReference objectAtIndex:0];
// checkView.tag = [[imgAndTagReference objectAtIndex:1] intValue];
// loop
for (UIButton *checkView in [self.tblImage subviews] )
{ i++;
NSLog(#"Checking tag: %d against passed in tag %d",checkView.tag, [[imgAndTagReference objectAtIndex:1] intValue]);
if ([checkView tag] == [[imgAndTagReference objectAtIndex:1] intValue]) {
if (i==35) break;
// Found imageView from tag, update with img
// [checkView setImage:[imgAndTagReference objectAtIndex:0]];
[checkView setImage:[imgAndTagReference objectAtIndex:0] forState:UIControlStateNormal];
//set contentMode to scale aspect to fit
checkView.contentMode = UIViewContentModeScaleAspectFit;
//change width of frame
CGRect frame = checkView.frame;
frame.size.width = 80;
checkView.frame = frame;
}
}
// release the pool
[pool release];
// Remove the activity indicator created in ViewDidLoad()
[self.activityIndicator removeFromSuperview];
}
the all code works perfect but i can't find the table cell subview here for (UIButton *checkView in [self.tblImage subviews] so how to find the subviews of table cell subviews.?
i want to create something like this pls see image.! after that new city will come and just section change the data will show new images in row and cell section.
you may use SDWebImage
Web Image
This library provides a category for UIImageVIew with support for remote images coming from the web.
It provides:
An UIImageView category adding web image and cache management to the Cocoa Touch framework
An asynchronous image downloader
An asynchronous memory + disk image caching with automatic cache expiration handling
A guarantee that the same URL won't be downloaded several times
A guarantee that bogus URLs won't be retried again and again
Performances!
just use it
[youImageView setImageWithURL:[NSURL URLWithString:url] placeholderImage:nil options:SDWebImageRetryFailed];
I need a UITableView whose first row should show a logo, while other rows should show the other table datas.
I tried the following code:
if(indexPath.row == 0) {
//Code for imageview
} else {
//Code to display array content
}
My problem is that I don't get the first array item in the table view.
Edited::
if (indexPath.row == 0) {
CGRect myImageRect = CGRectMake(120.0f, 5.0f, 70.0f, 55.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:#"logo.png"]];
[cell.contentView addSubview:myImage];
}
else {
NSDictionary *dict = [menuitems objectAtIndex:indexPath.row];
cell.textLabel.text =[dict objectForKey:#"category"];
}
Try to use an headerview. Use the following code to place logo at the top of row.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
UIImageView *myimgvw = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
[myimgvw setImage:[UIImage imageNamed:#"logo.png"]];
[myView addSubview:myimgvw];
return myView;
}
You haven't shown your code that displays the array content, but I guess you are using something like
[dataArray objectAtIndex:indexPath.row];
But because you are showing the image at row 0, you need to offset this by -1. In other words, the second row in your table will have indexPath.row = 1, but you need that to be objectAtIndex:0.
Then you need to put use indexPath-1 to show text instead of indexPath in else part of above code snippet.