Custom tableview cell with 6 imageviews - objective-c

I have a very difficult problem. This is how my custom cell looks like.
|--------------------------------------------------------------------------|
| |
| |
| Imageview1 Imageview2 Imageview3 Imageview4 imageview5 |
| |
| |
| |
|--------------------------------------------------------------------------|
I have a core database with 20 images. What I want to do is to fill up all these imageviews with my images. So at the and I should have 5 rows with in each imageview a different image.
Here is my code for my cellforrowAtIndexpath
- (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];
}
for(int i=0;i<5;i++)
{
float xCoord = 0.0;
Team *team = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:team.image]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
UIImageView* imgView = [[UIImageView alloc] initWithImage:image];
[imgView setFrame:CGRectMake(xCoord,0,imgView.frame.size.width, imgView.frame.size.height)];
[cell.contentView addSubview:imgView];
xCoord += imgView.frame.size.width;
}
return cell;
}
This is how far i got. I don't now how to fill up this tableview correctly. The other imageviews are name img_Player2,img_Player3,img_Player4,img_Player5.
Can anybody help?
Thank you
SCREENS WITH WHAT I WANT TO ACHIEVE
This is what I want to achieve:
And at the moment I have this.

You need to add the UIImageViews to the contentView of the UITableViewCell. Let's assume your 5 images are loaded in an NSArray called imagesArray. For simplicity I'm assuming each of your images is 64 (320/5) pixels wide and 44 pixels high.
Your code inside cellForRowAtIndexPath should roughly look like this:
float xCoord = 0.0;
for (UIImageView *imgView in imagesArray)
{
[imgView setFrame:CGRectMake(xCoord,0,imgView.frame.size.width, imgView.frame.size.height)];
[cell.contentView addSubview:imgView];
xCoord += imgView.frame.size.width;
}
Your code will be this:
float xCoord = 0.0;
for(int i=0;i<5;i++)
{
Team *team = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:team.image]];
UIImageView* imgView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:imageData]];
[imgView setFrame:CGRectMake(xCoord,0,imgView.frame.size.width, imgView.frame.size.height)];
[cell.contentView addSubview:imgView];
xCoord += imgView.frame.size.width;
}

Related

Prison Cells? Customizing IOS Table View Cells

I have the following code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIImage * i1 = [UIImage imageNamed: #"inc_01.png"];
UIImage * i2 = [UIImage imageNamed: #"inc_02.png"];
UIImage * i3 = [UIImage imageNamed: #"inc_04.png"];
UIImage * i5 = [UIImage imageNamed: #"inc_05.png"];
UIImage * i6 = [UIImage imageNamed: #"inc_06.png"];
UIImage * i7 = [UIImage imageNamed: #"inchd.png"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if(indexPath.row == 0)
{
UIImageView * header= [[UIImageView alloc] initWithImage: i1];
cell.backgroundView = header;
// Configure the cell…
}
else if (indexPath.row == 2)
{
UIImageView *backgroundCellImage=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 11)];
backgroundCellImage.image=[UIImage imageNamed:#"inc_06.png"];
[cell.contentView addSubview:backgroundCellImage];
}
else
{
// Configure the cell…
UIImageView *imageView = [[UIImageView alloc] initWithImage: i3];
cell.textLabel.text = #"text";
UIImageView *backgroundCellImage=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 57, 46)];
backgroundCellImage.image=[UIImage imageNamed:#"inc_02.png"];
UIImageView *backgroundCellImage2=[[UIImageView alloc] initWithFrame:CGRectMake(223, 0, 57, 46)];
backgroundCellImage2.image=[UIImage imageNamed:#"inc_04.png"];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(57, 0, 166, 46)];
label.text = #"wow";
[cell.contentView addSubview:backgroundCellImage];
[cell.contentView addSubview:backgroundCellImage2];
[cell.contentView addSubview:label];
}
return cell;
}
that basically creates a table view and puts an image to the left and right of each cell. I want it so that people can click on the left or right image in each cell, and something different happens based on the cell number.
So if they click on the left image for cell in row 1, a function gets call with the row number they clicked on, and an indicator telling me they clicked on the left image and not the right image.
How can I do that using objective-c?
Make a custom prototype cell as follows
---------------------------------------
| Button 1 | Text | Button 2 |
---------------------------------------
Call different methods for both the buttons.
Alternatively, you can have a look at this official documentation which explains cells in detail
Add right image as an accessory view and left image as an editing control

UITableViewCell with images in different dimensions

I'm trying to use my ReusableCell for cells with images in different dimensions. The images are put inside a 220x150 black box with with scaling UIViewContentModeScaleAspectFit.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"NewsTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NewsItem *item = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:item.imageUrl]];
[cell.imageView setImage:[[UIImage alloc] initWithData:data]];
[cell.imageView setBackgroundColor:[UIColor blackColor]];
[cell.imageView setContentMode:UIViewContentModeScaleAspectFit];
CGRect imageViewFrame = cell.imageView.frame;
imageViewFrame.size.width = 220;
imageViewFrame.size.height = 150
[cell.imageView setFrame:imageViewFrame];
[cell.textLabel setText:item.title];
return cell;
}
The above code results in a layout like below and the images are sometimes changing when scrolling in the table view.
Instead of this unstructured layout, I would like the images to be aligned like this:
What am I doing wrong with this ReusableCell?
EDIT1:
I'm trying to create an imageView and add this imageView as a superview to cell.contentView.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"NewsTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NewsItem *item = [self.fetchedResultsController objectAtIndexPath:indexPath];
UIImage *placeholderImage = [UIImage imageNamed:#"ImagePlaceholderThumb"]; //220x150
UIImageView *imageView = [[UIImageView alloc] initWithImage:placeholderImage];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:item.imageUrl]];
[imageView setImage:[[UIImage alloc] initWithData:data]];
[imageView setBackgroundColor:[UIColor blackColor]];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
CGRect imageViewFrame = imageView.frame;
imageViewFrame.size.width = placeholderImage.size.width;
imageViewFrame.size.height = placeholderImage.size.height;
[imageView setFrame:imageViewFrame];
[cell.contentView addSubview:imageView];
[cell.textLabel setText:item.title];
return cell;
}
The above code results in the following:
It is like some of the images are visible in two cells. It seems that they are not keeping the size I've set in the imageViewFrame. Do you know why?
A quick fix would be using content mode UIViewContentModeScaleAspectFill. Images will be stretched in one or both dimensions to fill up the whole image view bounds.
You really need subclassing UITableViewCell to do this right.
Thre is a lazy solution adding a new UIImageView and using a spacer, as Keller told you in his answer (feel free to accept his answer, this is just the missing code).
Extract of tableView:cellForRowAtIndexPath::
...
cell.textLabel.text = [NSString stringWithFormat:#"Cell #%i", indexPath.row];
cell.imageView.image = [UIImage imageNamed:#"spacer.png"]; /* spacer is 64 x 44 */
/* image view width should be ~ 64 px, otherwise it will overlap the text */
UIImageView *iv = [[UIImageView alloc] initWithFrame:(CGRect){.size={64, tableView.rowHeight}}];
switch (indexPath.row) {
case 0:
iv.image = [UIImage imageNamed:#"waterfall.png"];
break;
/* etc... */
}
if (indexPath.row < 3) {
/* add black bg to cell w/ images */
iv.backgroundColor = [UIColor blackColor];
}
iv.contentMode = UIViewContentModeScaleAspectFit;
[cell.contentView addSubview:iv];
...
The table will look like this:
You need to set the placeholder (spacer.png above) in the existing cell image view. It will push the text label to the right.
You can use aspect fill and remove the background color bit:
iv.contentMode = UIViewContentModeScaleAspectFill;
The table will look wrong because the image is drawn outsite the bounds:
Just clip to bounds to get a better result:
iv.clipsToBounds = YES;
Create a UIImageView subview for each cell and it to the contentView. Each UIImageView contains an image with a consistent frame but with option UIViewContentModeScaleAspectFit. Then just Set the background color of the UIImageView to black.
I just confirmed this works, but you need to also create a placeholder spacer image to make sure the textLabel moves out of the way. Just make it the same dimensions of your image (with the letter boxing).
- (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];
//spacer
cell.imageView.image = [UIImage imageNamed:#"placeholder"];
//imageview
UIImageView *thumbnail = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 44)];
thumbnail.tag = kThumbTag;
thumbnail.backgroundColor = [UIColor blackColor];
thumbnail.contentMode = UIViewContentModeScaleAspectFit;
[cell.contentView addSubview:thumbnail];
}
// Configure the cell...
cell.textLabel.text = [NSString stringWithFormat:#"Cell %d", indexPath.row];
cell.imageView.frame = CGRectMake(0, 0, 80, 44);
UIImageView *thumb = (UIImageView*)[cell.contentView viewWithTag:kThumbTag];
if (indexPath.row == 0) {
[thumb setImage:[UIImage imageNamed:#"image1.png"]];
} else {
[thumb setImage:[UIImage imageNamed:#"image2.png"]];
}
return cell;
}
Obviously, this example isn't lazy loading the images (I didn't realize you were loading them from a URL). For that, I would use a subclass with EGOImageView or something of the like.

Conditional adding an image to custom cell.contentView property

I want to create a UITableViewCell with an image between the content and the accessory view, sonly if certain condition is met.
So I have to create a custom content view with two UILabel and UIImageView as described in "Table View Programming Guide for iOS".
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UILabel *mainLabel;
UILabel *secondLabel;
UIImageView *icon;
YOEvento *aux = [[self.eventosListsContainer objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// 1. Check if it is a favourtite to display the icon
if (aux.isFavourite) {
// Evento is favourite
// 1. Create the main label view
mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(20, 8, 248, 14)] autorelease];
mainLabel.tag = MAINLABEL_TAG;
mainLabel.font = [UIFont systemFontOfSize:13.0];
mainLabel.textColor = [UIColor darkGrayColor];
mainLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:mainLabel];
// Create the date label
secondLabel = [[[UILabel alloc] initWithFrame:CGRectMake(23, 24, 245, 11)] autorelease];
secondLabel.tag = SECONDLABEL_TAG;
secondLabel.font = [UIFont systemFontOfSize:11.0];
secondLabel.textColor = [UIColor lightGrayColor];
secondLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:secondLabel];
// Create the image
icon = [[[UIImageView alloc] initWithFrame:CGRectMake(268, 12, 24, 21)] autorelease];
icon.tag = ICON_TAG;
icon.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:icon];
} else {
// Evento is not favourite
// 1. Create the main label view
mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(20, 8, 282, 14)] autorelease];
mainLabel.tag = MAINLABEL_TAG;
mainLabel.font = [UIFont systemFontOfSize:13.0];
mainLabel.textColor = [UIColor darkGrayColor];
mainLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:mainLabel];
// Create the date label
secondLabel = [[[UILabel alloc] initWithFrame:CGRectMake(23, 24, 279, 11)] autorelease];
secondLabel.tag = SECONDLABEL_TAG;
secondLabel.font = [UIFont systemFontOfSize:11.0];
secondLabel.textColor = [UIColor lightGrayColor];
secondLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:secondLabel];
}
} else {
//
if (aux.isFavourite) {
mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG];
secondLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG];
icon = (UIImageView *)[cell.contentView viewWithTag:ICON_TAG];
} else {
mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG];
secondLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG];
}
}
// Load cell values
if (aux.isFavourite) {
mainLabel.text = aux.nombre;
secondLabel.text = #"16 de Octubre"; // ?????????????
icon.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"29-heart"
ofType:#"png"]];
} else {
mainLabel.text = aux.nombre;
secondLabel.text = #"16 de Octubre"; // ?????????????
}
return cell;
}
The problem is that the icon image is loaded randomly because it keeps showing when reusing cells.
I have to admit that I don't fully understand the reusing cells theory.
In my original implementation (using the imageView property) it was easy, I just check is the event was not a favorite to set cell.imageView = nil. But have no idea how to do with custom contentView.
Once you create your cell, you are wanting to re-arrange its contents - this is adding unnecessary complexity. Instead you should create two different cell types - one for a favourite and one for a non-favourite. Try something like this:
static NSString *CellIdentifierFavourite = #"CellFavourite";
static NSString *CellIdentifierNonFavourite = #"CellNonFavourite";
// Other setup goes here
YOEvento *aux = [[self.eventosListsContainer objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
if (aux.isFavourite) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierFavourite];
if (cell == nil) {
// Create favourite cell here
}
// Populate favourite cell here
} else {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierNonFavourite];
if (cell == nil) {
// Create non-favourite cell here
}
// Populate non-favourite cell here
}
// Rest of method goes below

Programmatically Add Image to TableView Cell

I have a UITableView that gets information from a server and publishes the data into a table view. Inside of each cell is the information from the server.
For the purpose of this example, let's say the information we get from the server are numbers: 1, 2, 3, 4.
What I want to do is add an image (programmatically because there are if statements involved, etc) to the left side of the cell, and the text (1, 2, etc) right next to it.
Basically, I want each cell to look like this:
_____________________________________
| (IMAGE) (TEXT) | --CELL 0
--------------------------------------
_____________________________________
| (IMAGE) 2 | --CELL 1
--------------------------------------
Please excuse my crude illustration. :)
here you go:
cell.imageView.image = [UIImage imageNamed:#"image.png"];
Swift:
cell.imageView?.image = UIImage(named: "image.png")
You can add both the image and the text to the UITableViewCell in your UITableViewController subclass like so:
- (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] autorelease];
}
[[cell imageView] setImage:anImage];
[[cell textLabel] setText:[NSString stringWithFormat:#"%d",[indexPath row]];
return cell;
}
There are a few "built-in" properties to UITableViewCell that you can take advantage of, such as the imageView, textLabel, and detailTextLabel. For more information, check out the Table View Programming Guide.
The Swift 2.0 version of the first answer is:
cell.imageView?.image = UIImage(named: "image.png")
UITableViewCell objects have an imageView property; you can just set this image view's image and it will automatically display the image in the right place with the title next to it.
// create a rectangle box for image view
UIImageView *iconImage = [[UIImageView alloc] init];
iconImage.frame = CGRectMake(12,4,53.5,53.5);
// Create a label for cells
UILabel *cellName = [[UILabel alloc] initWithFrame:CGRectMake(75,18,200,20)];
cellName.font = [self fontForBodyTextStyle];
NSString *cellNameStr = [NSString stringWithFormat:#"%#",self.tableCellNames[indexPath.row]];
// Select path row tab actions
switch (indexPath.row) {
case 0:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"condition.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 1:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"videos.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 2:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"provider.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 3:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"info.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
default:
NSAssert(NO, #"Unhandled value in cellForRowAtIndexPath");
break;
}

Objective C: How to implement ABTableViewCell?

I am currently facing serious scrolling performance issues with my UITableView. I have the following subviews in my custom cell
1 X UIImageView
2 X UILabel
3 X UIButtons
I read on the net that having multiple subviews in a custom cell will have serious implications to scrolling performance. I found ABTableViewCell but am not sure how to go about porting my current code over to it.
Snippet of my current code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* PlaceholderCellIdentifier = #"PlaceholderCell";
int row = [indexPath row];
SomeClass *thisClass = [self.someClassArray objectAtIndex:row];
CustomCell* cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];
if (cell == nil)
{
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:PlaceholderCellIdentifier] autorelease];
//Set Avatar imageview
UIImageView *avatarView = [[UIImageView alloc]initWithFrame:CGRectMake(5, CELL_SPACING,CELL_AVATAR_WIDTH, CELL_AVATAR_HEIGHT)];
avatarView.tag = 1;
//Set text Label
UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectZero];
textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:13.0];
[textLabel setLineBreakMode:UILineBreakModeWordWrap];
textLabel.numberOfLines = 0;
textLabel.tag = 2;
//Add button1
UICustomButton *button = [[UICustomButton alloc]init];
button.tag = 3;
[self.contentView addSubview:avatarView];
[self.contentView addSubview:textlabel];
[self.contentView addSubview:button];
}
//Set Avatar imageview
UIImageView *thisAvatarView = (UIImageView *)[self.contentView viewWithTag:1];
thisAvatarView.image = self.dataForCell.user.avatar;
//Set data to text Label
NSString *text = [NSString stringWithFormat:#"%#",self.dataForCell.text];
CGFloat answerLabelHeight = [CustomCell getHeightOfLabel:self.dataForCell ofType:#"XXX"];
UILabel *thisTextLabel = (UILabel*)[self.contentView viewWithTag:2];
[thisTextLabel setFrame:CGRectMake(CELL_TEXT_LEFT_MARGIN, CELL_SPACING+CELL_USERNAMELABEL_HEIGHT+questionLabelheight,CELL_CONTENT_WIDTH - CELL_TEXT_LEFT_MARGIN*1.5, answerLabelHeight)];
thisTextLabel.text = someData;
//Set data to button
UICustomButton *thisButton = (UICustomButton *)[self.contentView viewWithTag:3];
[thisButton setButtonWithAnswer:self.dataForCell buttonType:#"like" navcon:self.navcon andFrame:CGRectMake(CELL_TEXT_LEFT_MARGIN, totalCommentLabelHeight + CELL_SPACING*4, 45, CELL_BUTTON_HEIGHT)];
thisLikeButton.imageView.image = [UIImage imageNamed:#"xxxx.png"];
}
Sorry for the long code, but can anyone guide me along on how I can start to use ABTableViewCell for this implementation?