I inserted this gradient coce into my cellForRowAtIndexPath method, but it does weird things. when I continuesly scroll up and down the page, it paints over and over the cells, so page gets darker and darker. Shold I put this somewhere else?
- (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];
}
//NSString *cellValue=[variable object ]
// Configure the cell...
NSString *cellValue = [items objectAtIndex:indexPath.row];
if (indexPath.row==0){
cell.textLabel.text=cellValue;
//cell.textLabel.font=bold;
cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:11];
cell.textLabel.font = [UIFont boldSystemFontOfSize:(CGFloat)16];
}
else {
cell.textLabel.text=cellValue;
//cell.textLabel.font=bold;
cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:14];
}
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
UIColor *color = [UIColor colorWithHue:0.56f saturation:0.98 brightness:0.65 alpha:0.5];
UIColor *colorWhite = [UIColor colorWithHue:0.0f saturation:0.0 brightness:0.95 alpha:0.5];
UIColor *colorBlack = [UIColor colorWithHue:1 saturation:1 brightness:0 alpha:0.2];
myBackView.backgroundColor=color;
UIColor *textColor = [UIColor colorWithHue:0.0f saturation:0.0 brightness:0.95 alpha:1];
cell.selectedBackgroundView = myBackView;
cell.selectedTextColor=textColor;
[myBackView autorelease];
UIView *cellView = [[UIView alloc] initWithFrame:cell.frame];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = cellView.bounds;
gradient.startPoint = CGPointMake(0, 0.5);
gradient.endPoint = CGPointMake(1.0, 0.5);
gradient.colors = [NSArray arrayWithObjects:(id)[colorBlack CGColor], (id)[colorWhite CGColor], nil];
[cell.layer insertSublayer:gradient atIndex:0];
[cellView autorelease];
return cell;
}
Your code looks like isn't complete, but you're inserting a layer EVERY time the table view requests a cell.
You are calling the drawing code each time the cell appears in the view when the only time you need to do so is when it is allocated and init'ed. Move to between the brackets of the if statement and you shouldn't have a problem any more.
I know because I did the same thing last night…
This goes for any custom labels or any other drawing that you might want to do. Outside of that if statement goes any value setting for anything else like labels and background colors.
Good luck!
Related
I added a simple UITableView to my screen. However, I want the cells in it to display a bunch of custom UI elements (mostly view and labels). Since UITableViewCell does not give me a whole lot of opportunity to customize it freely, I decided to add all the elements I need as subviews of the cell. Here is my cellForRowAtIndexPath method:
- (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];
}
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 25.0f, 25.0f)];
view1.backgroundColor = [UIColor redColor];
view1.layer.shadowColor = [[UIColor blackColor] CGColor];
view1.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
view1.layer.shadowOpacity = 0.8f;
view1.layer.shadowRadius = 3.0f;
[cell addSubview:view1];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(85.0f, 5.0f, 100.0f, 15.0f)];
label1.text = #"dummy text 1";
label1.backgroundColor = [UIColor clearColor];
label1.textColor = [UIColor lightGrayColor];
label1.textAlignment = UITextAlignmentRight;
label1.font = [UIFont systemFontOfSize:12.0f];
label1.lineBreakMode = NSLineBreakByTruncatingTail;
label1.shadowOffset = CGSizeMake(0.0f, 1.0f);
label1.shadowColor = [UIColor blackColor];
[cell addSubview:label1];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(85.0f, 25.0f, 100.0f, 15.0f)];
label2.text = #"dummy text 2";
label2.backgroundColor = [UIColor clearColor];
label2.textColor = [UIColor lightGrayColor];
label2.textAlignment = UITextAlignmentRight;
label2.font = [UIFont systemFontOfSize:12.0f];
label2.lineBreakMode = NSLineBreakByTruncatingTail;
label2.shadowOffset = CGSizeMake(0.0f, 1.0f);
label2.shadowColor = [UIColor blackColor];
[cell addSubview:label2];
UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(85.0f, 45.0f, 100.0f, 15.0f)];
label3.text = #"dummy text 3";
label3.backgroundColor = [UIColor clearColor];
label3.textColor = [UIColor lightGrayColor];
label3.textAlignment = UITextAlignmentRight;
label3.font = [UIFont systemFontOfSize:12.0f];
label3.lineBreakMode = NSLineBreakByTruncatingTail;
label3.shadowOffset = CGSizeMake(0.0f, 1.0f);
label3.shadowColor = [UIColor blackColor];
[cell addSubview:label3];
return cell;
}
All the above code works fine as far as cell customization goes. The problem is that, when a cell leaves the visible area, its subviews are not being released. As I move the tableview up and down, the shadow on the added UIView gets darker and darker, and I am guessing the labels are not being released either.
How do I fix this problem? I guess I could subclass a UITableViewCell class, but I would just be adding the subviews within a cell's class. It doesn't seem to be a solution. Is there a way to make the cell release its subviews when it disappears or a reliable way to truly and freely customize a cell?
Thanks!
A few additional info:
I do not use IB (do everything programmatically)
I use ARC
I use Xcode 4.6
My SDK is iOS 6.1
You can make your code work in two ways.
The correct way is subclassing it and exposing added views as properties.
The wrong way would be to modify your code like this:
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
for (UIView *cellView in sell.view)
{
[cellView removeFromSuperVuew];
}
You notice yourself that the shadow is getting darker and darker. That is because it is added (among all the other subviews) each time you load (and not only when you create) your cell.
When you decide to sublass don't forget to imlement:
- (NSString *)reuseIdentifier
{
return #"hereGoesTeuseIdentifierThatYouWillUseForThissKindOfCell";
}
You should create a UITableViewCell subclass and do all the customization there.
The issue is that cells are recycled. Think about it. You have attached all those UIViews to the cell each time your initialize or reuse a cell. Simply moving the paren will result in attachment of views only at initialization.
- (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];
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 25.0f, 25.0f)];
view1.backgroundColor = [UIColor redColor];
view1.layer.shadowColor = [[UIColor blackColor] CGColor];
view1.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
view1.layer.shadowOpacity = 0.8f;
view1.layer.shadowRadius = 3.0f;
[cell addSubview:view1];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(85.0f, 5.0f, 100.0f, 15.0f)];
label1.text = #"dummy text 1";
label1.backgroundColor = [UIColor clearColor];
label1.textColor = [UIColor lightGrayColor];
label1.textAlignment = UITextAlignmentRight;
label1.font = [UIFont systemFontOfSize:12.0f];
label1.lineBreakMode = NSLineBreakByTruncatingTail;
label1.shadowOffset = CGSizeMake(0.0f, 1.0f);
label1.shadowColor = [UIColor blackColor];
[cell addSubview:label1];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(85.0f, 25.0f, 100.0f, 15.0f)];
label2.text = #"dummy text 2";
label2.backgroundColor = [UIColor clearColor];
label2.textColor = [UIColor lightGrayColor];
label2.textAlignment = UITextAlignmentRight;
label2.font = [UIFont systemFontOfSize:12.0f];
label2.lineBreakMode = NSLineBreakByTruncatingTail;
label2.shadowOffset = CGSizeMake(0.0f, 1.0f);
label2.shadowColor = [UIColor blackColor];
[cell addSubview:label2];
UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(85.0f, 45.0f, 100.0f, 15.0f)];
label3.text = #"dummy text 3";
label3.backgroundColor = [UIColor clearColor];
label3.textColor = [UIColor lightGrayColor];
label3.textAlignment = UITextAlignmentRight;
label3.font = [UIFont systemFontOfSize:12.0f];
label3.lineBreakMode = NSLineBreakByTruncatingTail;
label3.shadowOffset = CGSizeMake(0.0f, 1.0f);
label3.shadowColor = [UIColor blackColor];
[cell addSubview:label3];
}
return cell;
}
That said... If you want to make those cells interesting by adding content, you will need to remove them each time from the cell or hold a reference to the cells subviews. Final point, don't just remove all the views if you can help it. If the values are simply changing, subclass UITableViewCell and make your own custom class that let's you set the views unique values.
I have two UITableViews in my view which shows same contents(verses of bible) but in diffrent language,top tableview shows english and bottom-table shows hindi.everything works fine,but some chapters the verse loads the data in uitableview the application crashes,the error is in this area
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize textSize = [[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row] sizeWithFont:[UIFont fontWithName:#"Georgia" size:18.0 ] constrainedToSize:CGSizeMake(280.0f,MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
return textSize.height +20;
CGSize textSizehindi = [[tempArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont fontWithName:#"testfont" size:18.0 ] constrainedToSize:CGSizeMake(280.0f,MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
return textSizehindi.height +20;
}
and also after playing too much time in uitableview ,it cause the slow down of scrolling of UITableViewCells.And there is no smooth scrolling occours.Is there any eroor in above code which i get error while loading some chapters.
Thanks in advance.
EDIT:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
readCell *cell = (readCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"readCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
}
if(tableView == table)
{
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor colorWithRed:250.0 green:248.0 blue:192.0 alpha:1.0];
[myBackView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]];
cell.selectedBackgroundView = myBackView;
[myBackView release];
table.backgroundColor = [UIColor clearColor];
table.separatorColor = [UIColor clearColor];
cell.chapterAndVerse.text = [NSString stringWithFormat:#"%d",indexPath.row+1];
cell.chapterAndVerse.font = [UIFont fontWithName:#"Georgia" size:17.0];
cell.chapterAndVerse.frame=CGRectMake(0, 10, 30.0, 20.0);
cell.textLabel.text = [NSString stringWithFormat:#" %#",[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row]];
cell.textLabel.font = [UIFont fontWithName:#"Georgia" size:18];
cell.backgroundColor = [UIColor clearColor];
}
else if(tableView == tab)
{
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor colorWithRed:250.0 green:248.0 blue:192.0 alpha:1.0];
[myBackView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]];
cell.selectedBackgroundView = myBackView;
[myBackView release];
tab.backgroundColor = [UIColor clearColor];
tab.separatorColor = [UIColor clearColor];
cell.chapterAndVerse.text = [NSString stringWithFormat:#"%d",indexPath.row+1];
cell.chapterAndVerse.font = [UIFont fontWithName:#"Georgia" size:17.0];
cell.chapterAndVerse.frame=CGRectMake(0, 10, 30.0, 20.0);
cell.textLabel.text = [NSString stringWithFormat:#" %#",[tempArray objectAtIndex:indexPath.row]];
cell.textLabel.font = [UIFont fontWithName:#"testfont" size:18];
cell.backgroundColor = [UIColor clearColor];
}
return cell;
}
EDIT2
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == table) {
return [delegate.allSelectedVerseEnglish count];
}
else if (tableView == tab )
{
return [tempArray count];
}
}
first do one thing. before use of your both array, NSLog the both array before reloading table. And check both array have same number of objects. it may be a reason of crashing.
in the above code first add the conditions for returning the text size for english and hindi verse. because currently you are always returning the text size of first english verse.The main problem you are facing is not in your above code.The problem should be on your another table view delegate call:- CellForRowAtIndexPath.
Can you paste your CellForRowAtIndexPath delegate call here so that i can give you more idea.
2nd part of your heightForRowAtIndexPath method is never called. It allways returning return textSize.height +20; Most possible way to crash is null pointer delegate.allSelectedVerseEnglish or unexisting font.
I'm having a weird problem with my cells when it gets highlighted.
Just to introduce you better what the problem is, take a look at these two pictures:
This is how the cell looks like when it's not selected (normal state): http://cl.ly/0n193u3U1o403x1s0m3z
This is how the cell looks when it's highlighted (during tap): http://cl.ly/1o2U400D3L0b3n3m1N1J
As you can see, the background of the second label seems to get ignored when the cell is selected.
I don't want this to happen. I just want the 2nd label to stay there and remain as it is.
This is how I create the cells (there are several types each of them using different cell identifier).
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellTableIdentifier] autorelease];
cell.backgroundView = [[[UIView alloc] init] autorelease];
}
switch (indexPath.section) {
...
...
case kTableSectionPending:
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 10, self.tableView.frame.size.width - 20, 30.0)] autorelease];
UILabel *label2 = [[[UILabel alloc] initWithFrame:CGRectMake(10, 50, self.tableView.frame.size.width - 20, 30.0)] autorelease];
label.text = NSLocalizedString(#"IncompletePath", #"");
label.font = [UIFont boldSystemFontOfSize:16];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.layer.cornerRadius = 10;
FFRoute *lastPoint = ((FFRoute *) [[[FFSQLite sharedSingleton] getRoutesFromItinerary:itinerary] lastObject]);
label2.text = [NSString stringWithFormat:#"%#: %#", NSLocalizedString(#"LastPoint", #""), [self getStringFromTimestampOfFFRoute:lastPoint]];
label2.font = [UIFont systemFontOfSize:15];
label2.textAlignment = UITextAlignmentCenter;
label2.textColor = [UIColor whiteColor];
label2.backgroundColor = [UIColor colorWithRed:68/255.f green:82/255.f blue:124/255.f alpha:1];
label2.layer.cornerRadius = 10;
[label2 setOpaque:YES];
[cell.contentView addSubview:label];
[cell.contentView addSubview:label2];
// Set up background color
UIColor *bgcolor = [UIColor colorWithRed:250/255.f green:212/255.f blue:137/255.f alpha:1];
cell.backgroundView.backgroundColor = bgcolor;
break;
}
I tried to set to true the Opaque property with no luck.
What am I missing? Thank you
When you create the cell, do:
cell.selectionStyle = UITableViewCellSelectionStyleNone;
A cell can be highlighted, selected or both. You might want to customize your drawing so that it draws in a selected state (but draws as usual when only in highlighted state).
In iOS 6, you can use 2 methods in UITableViewDelegate to customise highlighting for the cell, and also its subviews:
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell setBackgroundColor:[UIColor lightGrayColor]]; // Your highlight color
// Make changes to subviews in cell
}
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[UIView animateWithDuration:0.5f animations:^{
[cell setBackgroundColor:[UIColor whiteColor]]; // Your unhighlight
// Revert back changes for subviews
}];
}
I need to do this: when a user select a row from an tableview to send the text from that row to a textfield from an other tableview.
For example, if I select Services from
I want to see Services here,near Type de Partenaire :
I tried this :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = selectedCell.textLabel.text;
NSLog(#"%#",cellText);
self.recherchePartenaireTableView.partenaire.text=cellText;
}
and when Button ok is pressed :
-(IBAction)OkButtonPressed:(id)sender{
NSLog(#"BTN Ok");
[self.recherchePartenaireTableView.tableviewrecherchepartenaire reloadData];
[self.navigationController popViewControllerAnimated:YES];
}
but this is not working. Can anyone help me? Thanks in advance..
in implementation file for first image
- (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.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundView.opaque = NO;
//cell.alpha = 0.65;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.opaque = NO;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:18];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.opaque = NO;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
}
// Set up the cell...
[[cell textLabel] setText: [typePartenaireArray objectAtIndex:indexPath.row]] ;
return cell;
}
** in implementation file for second image**
- (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.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundView.opaque = NO;
//cell.alpha = 0.65;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.opaque = NO;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:18];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.opaque = NO;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
if(indexPath.row==0)
{
partenaire=[[UITextField alloc] init];
partenaire.frame = CGRectMake(200,10,80,50);
partenaire.textColor=[UIColor grayColor];
partenaire.text=#"Tous";
[partenaire setKeyboardAppearance:NO];
[cell.contentView addSubview:partenaire];
}
}
// Set up the cell...
[[cell textLabel] setText: [arraytableview objectAtIndex:indexPath.row]] ;
return cell;
}
Override the init method of the second UITableView to accept an extra argument (in your case the text of the cell selected in the current UITableView). And in the second UITableView where you configure the cells, set their text as you wish with this parameter you just received from the previous tableView which the second tableview object was alloc initted.
You have to insert your text into your array arraytableview in position 1 (arrays start at 0). Make sure it is declared as NSMutableArray otherwise you can't change it.
So you have to do: [arraytableview insertObject: cellText atIndex: 1] in your OK method. For this you might have to introduce a variable that holds the currently selected text to "transfer" it from the selected method to the OK method.
Or you just add it in your - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath, but then it would add the text immediately without pressing OK. really depends what you want
I hope it's clear now.
Code to define UITableViewCell with multiple lines, textfields, etc:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
imageView = [[UIImageView alloc] initWithFrame: CGRectZero];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.contentView addSubview: imageView];
titleLabel = [[UILabel alloc] initWithFrame: CGRectZero];
[titleLabel setFont:[UIFont systemFontOfSize:14.0]];
[titleLabel setTextColor:[UIColor blackColor]];
[titleLabel setHighlightedTextColor:[UIColor darkGrayColor]];
[titleLabel setLineBreakMode: UILineBreakModeWordWrap];
titleLabel.numberOfLines = 2;
[self.contentView addSubview: titleLabel];
description = [[UITextField alloc] initWithFrame: CGRectZero];
[description setFont:[UIFont systemFontOfSize:12.0]];
[description setTextColor:[UIColor darkGrayColor]];
[description setHighlightedTextColor:[UIColor darkGrayColor]];
[self.contentView addSubview: description];
}
return self;
}
-(void) layoutSubviews {
[super layoutSubviews];
// D_IN;
[imageView setFrame:CGRectMake(8.0, 10.0, 20.0, 20.0)];
[titleLabel setFrame:CGRectMake(40.0, 1.0, 250.0, 40.0)]; //two lines
[description setFrame:CGRectMake(40.0, 37.0, 250.0, 3.0)];//not used
// D_OUT;
}
Make sure that the layout is correct when specifying the starting x and y for each new component otherwise you won't see anything!!!
Then just set it with
titleLabel.text =
or
description.text =
So I have some issues with my tableview. I have a custom label that I put into a tableview cell to add a little better graphics than the standard UItableviewcell. However, I was running into my first problem,
the text labels that I had on the cells were changing with and over writing each other upon scrolling, only when the cells had moved off screen and then came back. Upon some research I found that maybe it had something to do with dequeueReusableCellWithIdentifier: so I adjusted my code. this is where problem two comes in.
When I load the table everything is in its right place, correct looking and all. However when I start to scroll down I can get to all of my cells except the last one, it will go to the very bottom of the 8th cell and freeze, but I should have 9 cells loaded.
I am quite confused by some of this, could anyone provide some code or guidance to help me along?
Thanks.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Run");
CoCoachAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
static NSString *CellIdentifier = #"Cell";
UILabel *label;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSArray *keys = [[appDelegate rowersDataStore] allKeys];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// Configure the cell...
label = [[[UILabel alloc] initWithFrame:CGRectMake(20, 15, cell.bounds.size.width - 10, 30)] autorelease];
label.font = [UIFont boldSystemFontOfSize:16];
label.backgroundColor = [UIColor clearColor];
label.shadowColor = [UIColor colorWithWhite:1.0 alpha:0.5];
label.shadowOffset = CGSizeMake(0,1);
label.textColor = [UIColor colorWithRed:0x4c/255.0 green:0x4e/255.0 blue:0x48/255.0 alpha:1.0];
switch (indexPath.section) {
case 0:
label.frame = CGRectMake(0, 15, cell.bounds.size.width - 10, 30);
label.textAlignment = UITextAlignmentCenter;
break;
case 1:
label.textAlignment = UITextAlignmentLeft;
UIImage *accessoryImage = [UIImage imageNamed:#"content_arrow.png"];
UIImageView *accessoryView = [[UIImageView alloc] initWithImage:accessoryImage];
cell.accessoryView = accessoryView;
[accessoryView release];
break;
}
UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.frame];
UIImage* img = [UIImage imageNamed:#"odd_slice.png"];
imgView.image = img;
cell.backgroundView = imgView;
[imgView release];
//Selected State
UIImage *selectionBackground = [UIImage imageNamed:#"row_selected.png"];
UIImageView *selectionView = [[UIImageView alloc] initWithFrame:cell.frame];
selectionView.image = selectionBackground;
cell.selectedBackgroundView = selectionView;
[selectionView release];
}
switch (indexPath.section) {
case 0:
[label setText:#"Click to add new rower"];
break;
case 1:
[label setText:[[[appDelegate rowersDataStore] objectForKey:[keys objectAtIndex:indexPath.row]] objectForKey:#"Name"]];
break;
}
//Adds Text
[cell addSubview:label];
return cell;
}
I see several issues here. First, the general structure of this method should be...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
// Attempt to dequeue the cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// If cell does not exist, create it, otherwise customize existing cell for this row
if (cell == nil) {
// Create cell
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// Configure cell:
// *** This section should configure the cell to a state independent of
// whatever row or section the cell is in, since it is only executed
// once when the cell is first created.
}
// Customize cell:
// *** This section should customize the cell depending on what row or section
// is passed in indexPath, since this is executed every time this delegate method
// is called.
return cell;
}
Basically, UITableView uses a single UITableViewCell instance to draw every cell in the table view. So, when you first create this cell, you should configure it to a state that is common to all cells that will use this instance, independent of whatever row or section is passed in indexPath. In your example, this involves creating the label, image, and background image instances and adding them as subviews to the cell.
Once the cell is created (aka outside the if (cell == nil) statement), you should customize its properties according to how the cell should look for the specific row and section contained in indexPath. Since you want to access your custom label in this part of the code, I assigned a tag value to it so that we can access it beyond the code segment where it was created using viewWithTag:. Once we have the label, we can customize it according to the section as well as do anything else we want, such as customize the accessory view.
I slightly modified/cleaned up your code below. This is by far not the most efficient or elegant way to do what you want to do, but I was trying to keep as much of your code as possible. I haven't tested this, but if you try it it should work:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Run");
CoCoachAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSArray *keys = [[appDelegate rowersDataStore] allKeys];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// Configure the cell...
UILabel *label;
label = [[[UILabel alloc] initWithFrame:CGRectMake(20, 15, cell.bounds.size.width - 10, 30)] autorelease];
label.font = [UIFont boldSystemFontOfSize:16];
label.opaque = NO;
label.backgroundColor = [UIColor clearColor];
label.shadowColor = [UIColor colorWithWhite:1.0 alpha:0.5];
label.shadowOffset = CGSizeMake(0,1);
label.textColor = [UIColor colorWithRed:0x4c/255.0 green:0x4e/255.0 blue:0x48/255.0 alpha:1.0];
label.tag = 100;
[cell addSubview:label];
[label release];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.frame];
UIImage* img = [UIImage imageNamed:#"odd_slice.png"];
imgView.image = img;
cell.backgroundView = imgView;
[imgView release];
//Selected State
UIImage *selectionBackground = [UIImage imageNamed:#"row_selected.png"];
UIImageView *selectionView = [[UIImageView alloc] initWithFrame:cell.frame];
selectionView.image = selectionBackground;
cell.selectedBackgroundView = selectionView;
[selectionView release];
}
UILabel *lbl = (UILabel *)[cell viewWithTag:100];
switch (indexPath.section) {
case 0:
cell.accessoryView = nil;
lbl.frame = CGRectMake(0, 15, cell.bounds.size.width - 10, 30);
lbl.textAlignment = UITextAlignmentCenter;
[label setText:#"Click to add new rower"];
break;
case 1:
UIImage *accessoryImage = [UIImage imageNamed:#"content_arrow.png"];
UIImageView *accessoryView = [[UIImageView alloc] initWithImage:accessoryImage];
cell.accessoryView = accessoryView;
[accessoryView release];
lbl.frame = CGRectMake(20, 15, cell.bounds.size.width - 10, 30);
lbl.textAlignment = UITextAlignmentLeft;
[lbl setText:[[[appDelegate rowersDataStore] objectForKey:[keys objectAtIndex:indexPath.row]] objectForKey:#"Name"]];
break;
}
return cell;
}