This code shows how to add a scrollview to a cell and works perfect but I want to load a scrollview which I have designed in XIB and which is paging enabled, I tried directly loading the scrollview as a contentview to cell but it doest appear proper and doesnt scroll horizontally which it does outside the tableview cell, please guide me what changes will be required
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"ScrollCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 25, 320, 25)];
scroller.showsHorizontalScrollIndicator = NO;
UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320*4, 25)];
contentLabel.backgroundColor = [UIColor blackColor];
contentLabel.textColor = [UIColor whiteColor];
NSMutableString *str = [[NSMutableString alloc] init];
for (NSUInteger i = 0; i < 100; i++) { [str appendFormat:#"%i ", i]; }
contentLabel.text = str;
[scroller addSubview:contentLabel];
scroller.contentSize = contentLabel.frame.size;
[cell addSubview:scroller];
}
// cell.textLabel.text = [NSString stringWithFormat:#"cell #%i", indexPath.row];
return cell;
}
I am adding some other UI components to my XIB scrollview at runtime like button etc. code is as below
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; 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 grayColor];
[self.scrollView addSubview:subview];
// add buttons
CGRect Frame= CGRectMake(frame.origin.x,frame.origin.y,200,50);
//set your x and y position whatever u want.
UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom];
Button.frame = Frame;
UIImage *Img = [UIImage imageNamed:#"second.png"];
[Button setBackgroundImage:Img forState:UIControlStateNormal];
[scrollView addSubview: Button];
// add text
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(frame.origin.x,frame.origin.y+60,200,50)];
label.textAlignment = UITextAlignmentCenter;
label.text = #"Here you write your text";
label.textColor = [UIColor blackColor];
[scrollView addSubview:label ];
[subview release];
Please suggest me the modifications with which i can load my designed scrollview to tableview cell
Thanks a lot !
Add All Controls (such like label, scrollView, etc..) in cell.contentView instead of cell addSubview.
Related
I m trying to add cells to a tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
[cell setSelectionStyle:UITableViewCellEditingStyleNone];
// Set up the cell
cell.backgroundColor = [UIColor clearColor];
UIButton *buyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImageView *img;
UILabel *lbl;
UIImageView *backImage;
UILabel *textLabel;
UILabel *detailTextLabel;
NSInteger val = [indexPath row] * 3;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
//*buyBtn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)];
[buyBtn setImage:[UIImage imageNamed:#""] forState:UIControlStateNormal];
//*img = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];
[img setImage:[UIImage imageNamed:#""]];
//*lbl = [[UILabel alloc] initWithFrame:CGRectMake(x,y,w,h)];
}
else
{
backImage = [[UIImageView alloc] initWithFrame:CGRectMake(2, 4, 316, 62)];
[buyBtn setFrame:CGRectMake(257, 100, 57, 24)];
img = [[UIImageView alloc] initWithFrame:CGRectMake(257, 75, 57, 24)];
lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 57, 24)];
textLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 230, 25)];
detailTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 25, 230, 30)];
backImage.image = [UIImage imageNamed:#"shop-bg.png"];
[buyBtn setImage:[UIImage imageNamed:#"buy_button"] forState:UIControlStateNormal];
[img setImage:[UIImage imageNamed:#"price_button.png"]];
lbl.center = img.center;
lbl.textAlignment = UITextAlignmentCenter;
lbl.text = [self.original_List objectAtIndex:val+2];
lbl.backgroundColor = [UIColor clearColor];
textLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:14];
textLabel.backgroundColor = [UIColor clearColor];
textLabel.text = [self.original_List objectAtIndex:val];
detailTextLabel.text = [self.original_List objectAtIndex:val+1];
detailTextLabel.backgroundColor = [UIColor clearColor];
detailTextLabel.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:10];
textLabel.textColor = [UIColor blackColor];
detailTextLabel.textColor = [UIColor whiteColor];
detailTextLabel.numberOfLines = 2;
}
[buyBtn setTag:[indexPath row]];
[buyBtn addTarget:self action:#selector(buyBtnPressed:) forControlEvents:UIControlEventTouchDown];
[cell addSubview:backImage];
[cell addSubview:detailTextLabel];
[cell addSubview:textLabel];
[cell addSubview:buyBtn];
[cell addSubview:img];
[cell addSubview:lbl];
return cell;
}
From figure it can be seen that The first Cell DOES NOT Contain the added image, label and button, however if other cells are scrolled those who lost view turn transparent while those who are in view are okay.
Why is my three subview not getting added from first cell, my textLabel and detailTextLabel are added as they should.
First of all you don't dequeue the cell and always create a new one without reusing. Try adding something like this (not sure if you need autorelease, depends on weather you use ARC or not):
RecentsTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[RecentsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellIdentifier] autorelease];
}
Second, if you are not using ARC (auto ref counting) then you have bunch of lost refs here.
[[Class alloc] init] adds the ref and addSubview also adds the ref. I'm wondering what xcode "analyse" build says on this.
Third, imho all labels you added to the cell need also their "frame" property to set up, not just number of lines, etc.
here is example of how I did it. I created cell class derived what what you need (CopyableCell in my case).
This is *.h file.
#import <UIKit/UIKit.h>
#import "CopyableCell.h"
#interface RecentsTableViewCell : CopyableCell
#property (readonly, retain) UILabel *titleLabel;
#property (readonly, retain) UILabel *detailsLabel;
#property (readonly, retain) UILabel *subdetailsLabel;
#property (readonly, retain) UILabel *subtitleLabel;
#end
Here is *.m file.
#import "RecentsTableViewCell.h"
#interface RecentsTableViewCell ()
// Redeclare property as readwrite
#property (readwrite, retain) UILabel *titleLabel;
#property (readwrite, retain) UILabel *subtitleLabel;
#property (readwrite, retain) UILabel *detailsLabel;
#property (readwrite, retain) UILabel *subdetailsLabel;
#end
#implementation RecentsTableViewCell
#synthesize titleLabel;
#synthesize subtitleLabel;
#synthesize detailsLabel;
#synthesize subdetailsLabel;
- (void) layoutSubviews
{
int leftMargin = 30;
int rightMargin = 6;
[super layoutSubviews];
CGRect cellRect = self.contentView.frame;
int width = cellRect.size.width - (leftMargin + rightMargin);
CGSize constraintSize = CGSizeMake(300, MAXFLOAT);
CGSize titleSize = [titleLabel.text sizeWithFont:titleLabel.font
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeTailTruncation];
CGSize detailsSize = [detailsLabel.text sizeWithFont:detailsLabel.font
constrainedToSize:constraintSize
lineBreakMode:UILineBreakModeTailTruncation];
if (titleSize.width > titleLabel.frame.size.width ||
detailsSize.width > detailsLabel.frame.size.width) {
CGRect detailsFrame = detailsLabel.frame;
detailsFrame.size.width = detailsSize.width;
detailsFrame.origin.x = cellRect.size.width -
(detailsSize.width + subdetailsLabel.frame.size.width + rightMargin);
CGRect titleFrame = titleLabel.frame;
titleFrame.size.width = width - detailsSize.width - subdetailsLabel.frame.size.width - 5;
titleLabel.frame = titleFrame;
detailsLabel.frame = detailsFrame;
}
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
CGRect cellRect = self.contentView.frame;
cellRect.size.height = 61;
self.contentView.frame = cellRect;
int leftMargin = 30;
int rightMargin = 6;
int x = cellRect.origin.x + leftMargin;
int y = cellRect.origin.y;
int width = cellRect.size.width - (leftMargin + rightMargin);
int height = cellRect.size.height;
int titleWidth = width * 0.75;
int alldetailsWidth = width - titleWidth;
int detailsWidth = alldetailsWidth * 0.65;
int subdetailsWidth = alldetailsWidth - detailsWidth;
int titleHeight = height * 0.38;
int subtitleHeight = height - titleHeight;
self.titleLabel = [[[UILabel alloc]
initWithFrame:CGRectMake(x, y, titleWidth, titleHeight)] autorelease];
self.titleLabel.font = [UIFont boldSystemFontOfSize:18.0f];
self.titleLabel.highlightedTextColor = self.textLabel.highlightedTextColor;
self.titleLabel.textAlignment = UITextAlignmentLeft;
self.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleBottomMargin;
[self.contentView addSubview:titleLabel];
self.detailsLabel = [[[UILabel alloc]
initWithFrame:CGRectMake(cellRect.size.width -
(detailsWidth + subdetailsWidth + rightMargin),
y + 4, detailsWidth, titleHeight - 4)] autorelease];
self.detailsLabel.font = [UIFont boldSystemFontOfSize:13.0f];
self.detailsLabel.textColor = [UIColor colorWithRed:0.12 green:0.439 blue:0.847 alpha:1];
self.detailsLabel.textAlignment = UITextAlignmentRight;
self.detailsLabel.highlightedTextColor = self.textLabel.highlightedTextColor;
self.detailsLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleHeight;
[self.contentView addSubview:detailsLabel];
self.subdetailsLabel = [[[UILabel alloc]
initWithFrame:CGRectMake(cellRect.size.width - (subdetailsWidth + rightMargin),
y + 6, subdetailsWidth, titleHeight - 6)] autorelease];
self.subdetailsLabel.font = [UIFont systemFontOfSize:11.0f];
self.subdetailsLabel.textColor = [UIColor colorWithRed:0.12 green:0.439 blue:0.847 alpha:1];
self.subdetailsLabel.textAlignment = UITextAlignmentCenter;
self.subdetailsLabel.highlightedTextColor = self.textLabel.highlightedTextColor;
self.subdetailsLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleHeight;
[self.contentView addSubview:subdetailsLabel];
self.subtitleLabel = [[[UILabel alloc]
initWithFrame:CGRectMake(x, y + titleHeight, width, subtitleHeight - 2)] autorelease];
self.subtitleLabel.highlightedTextColor = self.textLabel.highlightedTextColor;
self.subtitleLabel.font = [UIFont systemFontOfSize:13.0f];
self.subtitleLabel.textColor = [UIColor darkGrayColor];
self.subtitleLabel.numberOfLines = 2;
self.subtitleLabel.lineBreakMode = UILineBreakModeTailTruncation;
self.subtitleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleTopMargin;
[self.contentView addSubview:subtitleLabel];
self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return self;
}
- (id) init
{
return [self initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"RecentsCell"];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
- (void) dealloc
{
self.titleLabel = nil;
self.subtitleLabel = nil;
self.detailsLabel = nil;
self.subdetailsLabel = nil;
[super dealloc];
}
#end
Note that I'm not using ARC so if you use this code, modify it accordingly. Here is how I use this class in my table view controller.
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"TransactionCell";
NSMutableArray *data = searching ? searchResult : dataSource;
NSDictionary *object = [data objectAtIndex:[indexPath row]];
RecentsTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[RecentsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellIdentifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.indexPath = indexPath;
cell.delegate = self;
cell.titleLabel.text = #"Transaction title";
if (pendingTransaction) {
cell.titleLabel.textColor = [UIColor grayColor];
cell.titleLabel.font = [UIFont systemFontOfSize:cell.titleLabel.font.pointSize];
} else {
if (!isGreen)
cell.titleLabel.textColor = [UIColor colorWithRed:0.733 green:0.098
blue:0.098 alpha:1];
else
cell.titleLabel.textColor = cell.textLabel.textColor;
cell.titleLabel.font = [UIFont boldSystemFontOfSize:cell.titleLabel.font.pointSize];
}
cell.subtitleLabel.text = #"Transaction subtitle";
cell.detailsLabel.text = #"Some details like location";
cell.subdetailsLabel.text = #"200 USD";
return cell;
}
This produces table very similar to what you see in iphone call app, that is, title, subtitle, subdetails in blue at rightmost position of the cell, etc.
You can see it in Torchoo app in the appstore. It is free to use and you can use test account to log in and see it as example.
Hope this helps a bit.
Are you continuously adding subView to cell not removing any subView?
Check condition while adding subView like code not exact I have written:
if ([[cell.contentView subviews] count]==0) {
[self.contentView addSubview:imageView];
}
If cell content View doesn't contain that View then add it.
Previous answer is correct although there is more that you could fix.
The part of code that adds subviews should only be called in case when the cell is not dequeued. Otherwise you'll be adding multiple instances of the same buttons, views etc.
[cell addSubview:backImage];
[cell addSubview:detailTextLabel];
[cell addSubview:textLabel];
[cell addSubview:buyBtn];
[cell addSubview:img];
[cell addSubview:lbl];
Also I guess it is better to add to cell.contentView instead of directly to cell. So it should look something like this:
RecentsTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[RecentsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellIdentifier] autorelease];
/*Create your subviews here*/
[cell.contentView addSubview:backImage];
[cell.contentView addSubview:detailTextLabel];
[cell.contentView addSubview:textLabel];
[cell.contentView addSubview:buyBtn];
[cell.contentView addSubview:img];
[cell.contentView addSubview:lbl];
}
// Below just set properties of existing subviews of the cell: color, textColor, text etc.
// You need to retrieve them and one way is to assign them to properties in
// RecentsTableViewCell or set the tag property of the subviews and retrieve
// them using e.g. [cell.contentView viewWithTag:kBuyButtonTag].
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
...
}
else {
...
}
I have some weird behavior on the iPad that I am not getting on the iPhone.
I have a Grouped table view that has sections and headers for the sections, the problem is that on the iPad the top most section's header is not displayed, when scrolling down the table the sections header appears for a short while just before going of screen.
Before scrolling
http://desmond.imageshack.us/Himg525/scaled.php?server=525&filename=screenshot2012051410074.png&res=landing http://desmond.imageshack.us/Himg525/scaled.php?server=525&filename=screenshot2012051410074.png&res=landing
After scrolling
http://desmond.imageshack.us/Himg59/scaled.php?server=59&filename=screenshot2012051410074.png&res=landing http://desmond.imageshack.us/Himg59/scaled.php?server=59&filename=screenshot2012051410074.png&res=landing
The code for creating the headers of the sections:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil || [sectionTitle isEqualToString:#""]) {
return nil;
}
// Create label with section title
UILabel *label = [[UILabel alloc] init] ;
label.frame = CGRectMake(12, 0, 300, 30);
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:16];
label.text = sectionTitle;
UIImage *img = [UIImage imageNamed:#"header"];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 300, 44)];
imgView.image = img;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44)];
[view addSubview:imgView];
[view addSubview:label];
return view;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
int aSection = [[self.sectionsToDisplay objectAtIndex:section] integerValue];
return [self.groupHeadings objectAtIndex:aSection];
}
My TableView's code:
tableViewResult = [[UITableView alloc] initWithFrame:mainView.frame style:UITableViewStyleGrouped];
tableViewResult.separatorColor = [UIColor clearColor];
[mainView addSubview:tableViewResult];
I set the delegate and datasource in another method as I first do a web request before loading any data into the table, ie when the web request is done I do:
tableViewResult.delegate = self;
tableViewResult.dataSource = self;
[tableViewResult reloadData];
Everything works as expected except for the header of the top most section, and only on the iPad.
Any ideas what can cause this behavior?
What fixed the issue was changing this function to:
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
int aSection = [[self.sectionsToDisplay objectAtIndex:section] integerValue];
if([[self.groupHeadings objectAtIndex:aSection] isEqualToString:#""])
return nil;
return [self.groupHeadings objectAtIndex:aSection];
}
I have a problem with my UITableViewCell's. I am developing an application where certain posts in a feed may contain an image, and the button clicked will also need to contain a 'tag' number depending on which row of the table it is as I have an array of images downloaded.
The problem lies when I am scrolling (as you have already guessed) the table. The image loads fine, however when I scroll up/down the image will then go above other cells, and when the cell is shown back on the screen, the image will quickly change it's position to the proper position etc.
Here's the code for my UITableView:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [feed count];
}
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier
{
CGRect rect = [self.tableView bounds];
CGRect CellFrame = CGRectMake(0.0f, 0.0f, rect.size.width, 10.0f);
CGRect TitleFrame = CGRectMake(55.0f, 10.0f, 240.0f, 15.0f);
CGRect TextFrame = CGRectMake(55.0f, 25.0f, 250.0f, 15.0f);
CGRect PinFrame = CGRectMake(21.0f, 10.0f, 30.0f, 30.0f);
CGRect ViewFrame = CGRectMake(50.0f, 35.0f, 260.0f, 0.0f);
CGRect CommentsFrame = CGRectMake(300.0f, 10.0f, 20.0f, 15.0f);
UILabel * lblTemp;
UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
[cell setFrame:CellFrame];
lblTemp = [[UILabel alloc] initWithFrame:TitleFrame];
lblTemp.tag = 1;
lblTemp.numberOfLines = 0;
lblTemp.opaque = NO;
lblTemp.font = [UIFont systemFontOfSize:13.0f];
lblTemp.backgroundColor = [UIColor clearColor];
lblTemp.textColor = [UIColor blackColor];
lblTemp.textAlignment = UITextAlignmentLeft;
lblTemp.shadowColor = [UIColor whiteColor];
lblTemp.shadowOffset = CGSizeMake(0, 1);
[cell.contentView addSubview:lblTemp];
lblTemp = [[UILabel alloc] initWithFrame:TextFrame];
lblTemp.tag = 2;
lblTemp.numberOfLines = 0;
lblTemp.lineBreakMode = UILineBreakModeWordWrap;
lblTemp.adjustsFontSizeToFitWidth = NO;
lblTemp.font = [UIFont systemFontOfSize:13.0f];
lblTemp.textColor = [UIColor lightGrayColor];
lblTemp.backgroundColor = [UIColor clearColor];
lblTemp.shadowColor = [UIColor whiteColor];
lblTemp.shadowOffset = CGSizeMake(0, 1);
[cell.contentView addSubview:lblTemp];
lblTemp = [[UILabel alloc] initWithFrame:CommentsFrame];
lblTemp.tag = 5;
lblTemp.numberOfLines = 1;
lblTemp.font = [UIFont systemFontOfSize:13.0f];
lblTemp.textColor = [UIColor lightGrayColor];
lblTemp.backgroundColor = [UIColor whiteColor];
lblTemp.shadowColor = [UIColor whiteColor];
lblTemp.shadowOffset = CGSizeMake(0, 1);
[cell.contentView addSubview:lblTemp];
UIImageView * imgTemp = [[UIImageView alloc] initWithFrame:PinFrame];
imgTemp.tag = 3;
[cell.contentView addSubview:imgTemp];
UIView * viewTemp = [[UIView alloc] initWithFrame:ViewFrame];
viewTemp.tag = 4;
[viewTemp setBackgroundColor:[UIColor whiteColor]];
[cell.contentView addSubview:viewTemp];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = #"Cell";
UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//UITableViewCell * cell = [self getCellContentView:CellIdentifier];
if (cell == nil) {
cell = [self getCellContentView:CellIdentifier];
} else {
[cell.contentView viewWithTag:999];
}
cell.backgroundColor = [UIColor clearColor];
UILabel * title = (UILabel *)[cell viewWithTag:1];
UILabel * journey = (UILabel *)[cell viewWithTag:2];
UIImageView * pin = (UIImageView *) [cell viewWithTag:3];
UILabel * comments = (UILabel *)[cell viewWithTag:5];
title.text = [NSString stringWithFormat:#"%#", [[feed objectAtIndex:indexPath.row] objectForKey:#"comment"]];
[title sizeToFit];
title.frame = CGRectMake(title.frame.origin.x, title.frame.origin.y, 240, title.frame.size.height);
if ([[feed objectAtIndex:indexPath.row] objectForKey:#"weather_desc"] != [NSNull null] ||
[[feed objectAtIndex:indexPath.row] objectForKey:#"weather_temp"] != [NSNull null]) {
journey.text = [NSString stringWithFormat:#"It's %# and %#°C.", [[feed objectAtIndex:indexPath.row] objectForKey:#"weather_desc"], [[feed objectAtIndex:indexPath.row] objectForKey:#"weather_temp"]];
} else {
journey.text = [NSString stringWithFormat:#"%#", [[[feed objectAtIndex:indexPath.row] objectForKey:#"journey"] objectForKey:#"name"]];
}
journey.frame = CGRectMake(journey.frame.origin.x, 10 + title.frame.size.height, 240, 15);
if ([[feed objectAtIndex:indexPath.row] objectForKey:#"latitude"] != [NSNull null]) {
pin.image = [UIImage imageNamed:#"LocationPin.png"];
} else {
pin.image = [UIImage imageNamed:#"Pin.png"];
}
if ([[feed objectAtIndex:indexPath.row] objectForKey:#"image_file_size"] != [NSNull null]) {
NSString * text = [NSString stringWithFormat:#"%#", [[feed objectAtIndex:indexPath.row] objectForKey:#"comment"]];
CGFloat height = [text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(250, 1500) lineBreakMode:UILineBreakModeWordWrap].height;
UIImage * myImage = [images objectForKey:[NSString stringWithFormat:#"%i", indexPath.row]];
UIButton * button = [[UIButton alloc] initWithFrame:CGRectZero];
[button setBackgroundImage:myImage forState:UIControlStateNormal];
[button setFrame:CGRectMake(title.frame.origin.x + 55.0f, 40.0f + height, 250.0f, myImage.size.height)];
[button setTag:10 + indexPath.row];
[button addTarget:self action:#selector(imagePressed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
UIView * backView = (UIView *) [cell viewWithTag:4];
[backView setFrame:CGRectMake(title.frame.origin.x + 50.0f, 35.0f + height, 260.0f, myImage.size.height + 10.0f)];
} else {
UIView * backView = (UIView *) [cell viewWithTag:4];
[backView setFrame:CGRectZero];
[backView setBackgroundColor:[UIColor clearColor]];
}
comments.text = [NSString stringWithFormat:#"%i", [[[feed objectAtIndex:indexPath.row] objectForKey:#"comments"] count]];
UIImage * background = [UIImage imageNamed:#"CellBackground.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:background];
imageView.contentMode = UIViewContentModeScaleToFill;
[cell setBackgroundView:imageView];
return cell;
}
- (void)tableView:(UITableView *)tv didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView deselectRowAtIndexPath:indexPath animated:NO];
MilestoneView * milestoneView = [[MilestoneView alloc] initWithNibName:#"MilestoneView" bundle:nil];
[milestoneView setMilestone:[feed objectAtIndex:indexPath.row]];
[self.navigationController pushViewController:milestoneView animated:YES];
}
- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * text = [NSString stringWithFormat:#"%#", [[feed objectAtIndex:indexPath.row] objectForKey:#"comment"]];
CGFloat height = [text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(240, 1500) lineBreakMode:UILineBreakModeWordWrap].height;
if ([[feed objectAtIndex:indexPath.row] objectForKey:#"image_file_size"] != [NSNull null]) {
UIImage * myImage = [images objectForKey:[NSString stringWithFormat:#"%i", indexPath.row]];
height += myImage.size.height + 15.0f;
}
return height + 37;
}
I am also using Apple's inbuilt 'ARC' with iOS 5.0.
Kind regards,
Dominic
In this code you add button in cellForRowAtIndexPath , hence this is happen....
so , you add the code for button , backView in getCellContentView and fetch images from array in getCellContentView.
UIImage *myImage = [images objectForKey:[NSString stringWithFormat:#"%i", indexPath.row]];
then,
UIButton *button = [[UIButton alloc] initWithFrame:CGRectZero];
[button setBackgroundImage:myImage forState:UIControlStateNormal];
[button setFrame:CGRectMake(55.0f, 40.0f, 250.0f, myImage.size.height)];
change height , width & x,y co-or. what u want and
set cell index to button.
[button setTag:indexPath.row];
[button addTarget:self action:#selector(imagePressed:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
Let me know if you have any problem....
Replace following function header
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {
.
.
}
with this function
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier indexPathForButtonTag:(NSIndexPath*)indexPath {
.
.
}
Replace following line in cellForRowAtIndex
cell = [self getCellContentView:CellIdentifier];
With the
cell = [self getCellContentView:CellIdentifier indexPathForButtonTag:indexPath];
You dont need each button to have a different tag. You should not be adding any subviews to the cell in cellForRowAtIndexPath because you will inevitably end up adding subview on top of subview. I notice you are also adding an image view as the cell's background in this method - you should only do this when you are creating a new cell.
Im going to assume there is a good reason you are not using a xib file for this cell layout, so just add the button in your contentView method and give it a known tag.
In your button's action method you can find out which row of the table the button was in by using the locationInView: and indexPathForRowAtPoint: methods:
CGPoint hit = [sender locationInView:self.tableView];
NSIndexPath *hitRow = [self.tableView indexPathForRowAtPoint:hit];
Also, note that setting a view's .hidden property to YES is probably less effort that setting its frame to CGRectZero.
Ended up fixing the solution by doing the following simple code:
NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)[[sender superview]superview]];
I used the UIButton sender to get the cell that it was within. I also pulled out all the modifications of the .tag's and kept them at the same number.
I want to add more than one image in table view.
I can add one image using cell.imageview.image.
but how to add one more image.
And I also want to add buttons in all cells.
How can I do that ?
You should make your own UITableViewCell subclass. There are many tutorials for that:
http://iphone.zcentric.com/2008/08/05/custom-uitableviewcell/
http://www.icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/
http://www.e-string.com/content/custom-uitableviewcells-interface-builder
Including:
http://stackoverflow.com
http://www.google.com
Make your own UITableViewCell subclass.
Use a UITableView with a custom cell with whatever you want in it, load extra images and labels. To get the custom cell, create an IBOutlet to the cell and use this method.
[[NSBundle mainBundle] loadNibNamed:#"customCellView" owner:self options:nil];
To make a cell, make a new Nib/Xib file which is blank, make files owner the class with the cells, drag a UITableviewcell object out and put whatever objects you want on top of that view, set background as clear color and when you load the nib, enter all info into those images and labels. GL
Here is a block of code I use alot in my apps. Its not the fastest way to implement, but it gives you complete control over how the cell looks and whats in it. Using the code below you can see how my app looks. You have 2 buttons on one cell. They both do something different when pushed, and if the actual CELL is selected I do something else. I removed some of the code because im sure you dont care about what Im doing when the cell is selected, only how to get the buttons on, and know which one is pushed.
-(void)scheduleServiceButtonPressed:(id)sender {
//when the user click the button "service appt" on a table cell, we get the cellRow and use that to identify which car to show
ServiceApptViewController * vc = (ServiceApptViewController *)[[ServiceApptViewController alloc] init];
vc.cameFromGarageSelectionInt = [sender tag]; // here is the cell row that was selected.
[self.navigationController pushViewController:vc animated:YES];
[vc release];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [app.savedUserVehicleArray count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
cell = [self getCellContentView:CellIdentifier:indexPath.row];
[cell setBackgroundColor:[UIColor redColor]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSLog(#"out cells for index");
return cell;
}
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier:(int)cellIndex {
NSLog(#"in content");
vehicleForRow = [app.savedUserVehicleArray objectAtIndex:cellIndex];
//CGRect CellFrame = CGRectMake(0, 0, 300, 60);
CGRect CellFrame = CGRectMake(0, 0, 320, 70);
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];
// put a UIView underneath for coloring
UIView * view = [[UIView alloc] initWithFrame:CellFrame];
if ( cellIndex%2 == 0 ){
view.backgroundColor = [UIColor whiteColor];
}else{
//view.backgroundColor = [UIColor colorWithRed:0.98 green:0.92 blue:0.52 alpha:1.0];
view.backgroundColor = [UIColor colorWithRed:.238 green:.238 blue:0.238 alpha:.10];
}
[cell.contentView addSubview:view];
[view release];
if (vehicleForRow.isDefault && [vehicleForRow.isDefault compare:#"YES"]==0) {
//add green check mark if vehicle is default
UIImageView * bgimage3 = [[UIImageView alloc] initWithFrame:CGRectMake(245, 15, 40, 32)];
bgimage3.image = [UIImage imageNamed:#"greenCheckMark.png"];
[cell.contentView addSubview:bgimage3];
[bgimage3 release];
//default vehicle label
UILabel *lblTemp;
NSString * z = [NSString stringWithFormat:#"Default"];
NSString * s1 = z;
CGRect Label1Frame = CGRectMake(240, 43, 250, 25); // name
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
lblTemp.adjustsFontSizeToFitWidth = TRUE;
lblTemp.text = s1;
lblTemp.font = [UIFont boldSystemFontOfSize:12];
lblTemp.textColor = [UIColor blueColor];
lblTemp.backgroundColor = [UIColor clearColor];
[lblTemp setTextAlignment:UITextAlignmentLeft];
[cell.contentView addSubview:lblTemp];
}
else {
UIImageView * bgimage3 = [[UIImageView alloc] initWithFrame:CGRectMake(250, 15, 30, 24)];
bgimage3.image = [UIImage imageNamed:#"grayCheckMark.png"];
[cell.contentView addSubview:bgimage3];
[bgimage3 release];
UILabel *lblTemp;
NSString * z = [NSString stringWithFormat:#"Set As Default"];
NSString * s1 = z;
CGRect Label1Frame = CGRectMake(233, 38, 250, 25); // name
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
lblTemp.adjustsFontSizeToFitWidth = TRUE;
lblTemp.text = s1;
lblTemp.font = [UIFont boldSystemFontOfSize:8];
lblTemp.textColor = [UIColor grayColor];
lblTemp.backgroundColor = [UIColor clearColor];
[lblTemp setTextAlignment:UITextAlignmentLeft];
[cell.contentView addSubview:lblTemp];
}
// add service button to each cell
UIImage *image;
schedServiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
image = [UIImage imageNamed:#"tableServiceButton.png"];
[schedServiceButton setBackgroundImage:image forState:UIControlStateNormal];
schedServiceButton.frame = CGRectMake(5, 30, 97, 35);
[schedServiceButton setTag:cellIndex];//this is how we know which cell button was pressed
[schedServiceButton addTarget:self action:#selector(scheduleServiceButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
schedServiceButton.titleLabel.font = [UIFont systemFontOfSize:12];
[schedServiceButton.titleLabel setLineBreakMode:UILineBreakModeCharacterWrap];
[schedServiceButton setTitle:#"Schedule\nService Appt." forState:UIControlStateNormal];
schedServiceButton.titleLabel.textAlignment = UITextAlignmentCenter;
[cell.contentView addSubview:schedServiceButton];
//yes add owners manual button
viewOMButton = [UIButton buttonWithType:UIButtonTypeCustom];
image = [UIImage imageNamed:#"tableOMButton.png"];
[viewOMButton setBackgroundImage:image forState:UIControlStateNormal];
viewOMButton.frame = CGRectMake(105, 30, 97, 35);
[viewOMButton setTag:cellIndex];
[viewOMButton addTarget:self action:#selector(viewOwnersManualButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
viewOMButton.titleLabel.font = [UIFont systemFontOfSize:12];
[viewOMButton.titleLabel setLineBreakMode:UILineBreakModeCharacterWrap];
[viewOMButton setTitle:#"View\nOwner's Manual" forState:UIControlStateNormal];
viewOMButton.titleLabel.textAlignment = UITextAlignmentCenter;
[cell.contentView addSubview:viewOMButton];
//car description label
UILabel *lblTemp;
NSString * z = [NSString stringWithFormat:#"%# %# %#",vehicleForRow.userVehicleYear,vehicleForRow.userVehicleMake,vehicleForRow.userVehicleModel];
NSString * s1 = z;
CGRect Label1Frame = CGRectMake(10, 5, 250, 25); // name
//Initialize Label with tag 1.
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
lblTemp.adjustsFontSizeToFitWidth = TRUE;
lblTemp.text = s1;
lblTemp.font = [UIFont boldSystemFontOfSize:16];
lblTemp.textColor = [UIColor blueColor];
lblTemp.backgroundColor = [UIColor clearColor];
[lblTemp setTextAlignment:UITextAlignmentLeft];
[cell.contentView addSubview:lblTemp];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
}
I am not sure why my UITableViewCell's are repeating after the user scrolls?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
ChartlyCell *cell = (ChartlyCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
//cell = [[[ChartlyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell = [[[ChartlyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier andDelegate:self] autorelease];
}
ChartlyObject *myChartlyObject;
myChartlyObject = [self.items objectAtIndex:indexPath.row];
cell.usernameLabel.text = myChartlyObject.userName;
cell.bodyLabel.text = myChartlyObject.chart_message;
[cell.messageLabel setText: myChartlyObject.chart_message];
cell.timeLabel.text = [Utils toShortTimeIntervalStringFromStockTwits: myChartlyObject.created_at];
[cell.chartImage loadImageFromURL:[NSURL URLWithString:myChartlyObject.imageThumbnail]];
return cell;
}
This is how the cell is being created:
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andDelegate:(id<NLabelDelegate>)ndelegate{
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
// Initialization code
CGRect rect;
rect = CGRectMake(77, 5, 200, 20); //CGRectMake(10, 5, 200, 20);
usernameLabel = [[UILabel alloc] initWithFrame:rect];
//usernameLabel.backgroundColor = backgroundColor;
usernameLabel.font = [Utils getBoldSystemFontWithSize:14];
usernameLabel.textColor = [UIColor colorWithRed:.368 green:.741 blue:.784 alpha:1];
[self.contentView addSubview: usernameLabel];
rect = CGRectMake(277, 5, 38, 20);
timeLabel = [[UILabel alloc] initWithFrame:rect];
timeLabel.textAlignment = UITextAlignmentRight;
//timeLabel.backgroundColor = backgroundColor;
timeLabel.font = [Utils getBoldSystemFontWithSize:14];
timeLabel.textColor = [UIColor colorWithRed:.98 green:.65 blue:.01 alpha:1];
[self.contentView addSubview: timeLabel];
/*rect = CGRectMake(77, 25, 238, 68); //CGRectMake(10, 50, 238, 68);
bodyLabel = [[UILabel alloc] initWithFrame:rect];
bodyLabel.font = [Utils getSystemFontWithSize:10];
bodyLabel.numberOfLines = 3;
bodyLabel.lineBreakMode = UILineBreakModeWordWrap;
bodyLabel.textColor = [UIColor blackColor];
bodyLabel.backgroundColor = [UIColor clearColor];
[self.contentView addSubview: bodyLabel];*/
rect = CGRectMake(77, 25, 238, 68);
messageLabel = [[NLabel alloc] initWithFrame:rect andDelegate:ndelegate];
[messageLabel setOpaque:NO];
[messageLabel setBackgroundColor: [UIColor blackColor]];
//[messageLabel setTotalheight:68];
[self.contentView addSubview: messageLabel];
rect = CGRectMake(13, 10, 48, 48);
chartImage = [[AsyncImageView alloc]initWithFrame:rect];
[self.contentView addSubview: chartImage];
}
return self;
}
UPDATE:
What is odd, is that if I uncomment the bodyLabel block and comment out the messageLabel block the repeating cells don't appear anymore. I am not sure why and would still like to find a resolution
UPDATE #2
Only messageLabel gets repeated. All of the other labels do NOT repeat. Why is this?
Because you are re-using tablecells for proper memory management as you should be doing via dequeueReusableCellWithIdentifier :) This way you don't have a unique cell for every single cell you need. Instead you only have about 5 or 6 that get re-used over and over during scrolling.
When the cell comes back into view, you need to reset the state (i.e. update the info, labels, images, etc) of each cell based on whatever cell came into view.
One thing you can do is to remove all the views in the cell.contentView after you create the cell or dequeue it. Alternatively, you can create a custom cell with attributes for the labels that you can set to different values when you scroll down.