i did this but still if i scroll table with force data are shuffling
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
//
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
XMLAppDelegate *appDelegate=(XMLAppDelegate*)[UIApplication sharedApplication].delegate;
DetailXml *aDetail=[appDelegate.dxml objectAtIndex:indexPath.row];
switch(indexPath.section)
{
case 0:
NSLog(#" cell 1");
{
CGRect frame;
frame.origin.x = 70;
frame.origin.y = 15;
frame.size.height = 25;
frame.size.width = 320;
cLabel = [[UILabel alloc] initWithFrame:frame];
cLabel.textColor = [UIColor blackColor];
//[label setFont:[UIFont fontWithName:#"Helvetica-Bold" size:16]];
[cLabel setFont:[UIFont fontWithName:#"Verdana-Italic" size:14]];
cLabel.tag = Ca;
[cell.contentView addSubview:cLabel];
[cLabel release];
cLabel.backgroundColor = [UIColor clearColor];
cLabel.text = [NSString stringWithFormat:#"%#", aDetail.telnumber];
cLabel.backgroundColor = [UIColor clearColor];
cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"r1.png"]] autorelease];
counte=1;
}
break;
case 1:
{NSLog(#" cell 2");
//counte=0;
cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"n1.png"]] autorelease];
}
break;
You Really should cleanup youre code before posting here!
It is very hard to read and you have many errors, whitespace and commented-out-lines in it that you should remove..
Read http://developer.apple.com/iphone/library/documentation/userexperience/conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW15
It should help you get started with custom cells..
Basically all the customization should go in:
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
//Customization here:
}
The way you do it you are allocating labels every time a new cell is displayed instead of reusing..
It seems like youre cells are static. You only have one cell per section and dont need reusing the cells?
If so maybe it would be easier to create static cells with interfacebuilder
http://developer.apple.com/iphone/library/documentation/userexperience/conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW32
Related
I have added to some rows from table view an TextField like this :
- (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 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// cell.accessoryType = UITableViewCellAccessoryNone;
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==1)
{
code=[[UITextField alloc] init];
code.frame = CGRectMake(200,10,80,50);
code.textColor=[UIColor grayColor];
code.text=#"515800";
[cell.contentView addSubview:code];
}
if(indexPath.row==2)
{
ville=[[UITextField alloc] init];
ville.frame = CGRectMake(200,10,80,50);
ville.textColor=[UIColor grayColor];
ville.text=#"Paris";
[cell.contentView addSubview:ville];
}
if(indexPath.row==4)
{
nomdepartenaire=[[UITextField alloc] init];
nomdepartenaire.frame = CGRectMake(200,10,80,50);
nomdepartenaire.textColor=[UIColor grayColor];
nomdepartenaire.text=#"Alliantis Ttttt";
[cell.contentView addSubview:nomdepartenaire];
}
}
// Set up the cell...
[[cell textLabel] setText: [listData objectAtIndex:indexPath.row]] ;
return cell;
}
The problem is that the keyboard doesn't appear if I want to change the text from TextField. Why?
Most probably there is something in the way of the text field and that makes it not receiving the touch event. Adding the entire cellForRowAtIndexPath would probably help us understand better what's happenning. Also, you should add the texfield and other custom elements to cell.contentsView, not directly to the cell.
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;
}
i can show small image in table view but i want to load a png as a BG of each cells of my table view how to do that
cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil && searching==NO) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
/*
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"Live and LearnCell" ofType:#"png"]];
cell.image = img;
*/
//// FISRTS LABEL
cell.imageView.image = [UIImage imageNamed:#"TopCell.png"];
mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(70.0, 5.0, 220.0, 15.0)] autorelease];
mainLabel.tag = MAINLABEL_TAG;
// mainLabel.font = [UIFont systemFontOfSize:14.0];
[mainLabel setFont:[UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]]];
mainLabel.textAlignment = UITextAlignmentLeft;
mainLabel.textColor = [UIColor whiteColor];
mainLabel.highlightedTextColor = [UIColor greenColor];
//mainLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:mainLabel];
}
how to add PNG as BG to this cell??
cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]] autorelease];
Have a look at the backgroundView property of UITableViewCell. You can load your PNG into a UIImageView and set it as the background of your cell.
If you want to set the background of your UITableView, it also has a backgroundView property.
I am using custom table view cell to show to table view
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier])
{
m_mail_status = [[UIImageView alloc] initWithFrame:CGRectMake(295, 10, 18, 18)];
[self addSubview:m_mail_status];
m_subject = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 270, 37)];
UIFont* font = m_subject.font;
m_subject.font = [UIFont boldSystemFontOfSize:font.pointSize];
//m_subject.font = [UIFont systemFontOfSize:17];
[self addSubview:m_subject];
m_from = [[UILabel alloc] initWithFrame:CGRectMake(10, 37, 200, 15)];
m_from.font = [UIFont systemFontOfSize:12];
[self addSubview:m_from];
m_date = [[UILabel alloc] initWithFrame:CGRectMake(180, 37, 140, 15)];
m_date.textAlignment= UITextAlignmentRight;
m_date.font = [UIFont systemFontOfSize:12];
[self addSubview:m_date];
}
return self;
}
and my code in view controller,
static NSString *MyIdentifier = #"ProductsCell";
InboxViewCell *cell = (InboxViewCell*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
//static NSString *CellIdentifier = #"Cell";
// UITableViewCell *cell = nil;// [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"xx"] autorelease];
if(cell == nil)
cell = [[[InboxViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
cell.m_subject.text = [[inbox objectAtIndex:indexPath.row] objectForKey:#"subject"];
cell.m_from.text = [[inbox objectAtIndex:indexPath.row] objectForKey:#"from"];
cell.m_date.text = [[inbox objectAtIndex:indexPath.row] objectForKey:#"date"];
if([[[inbox objectAtIndex:indexPath.row] objectForKey:#"**reservation_status**"] isEqualToString:#"1"])
{
cell.m_mail_status.image = [UIImage imageNamed:#"reservation_symbol.png"];
}
It loads in proper manner when it first loads
The issue when i scroll down and move to up
all the image is shown it ignores the if statement result
Thanks in advance
Regards,
sathish
The reason for this is that the cells are reused, you will want to reset that image in the else of your if.
I'm wondering if it is possible to add cell content to a uitableview based on the row index?
For example if it is the first cell (row 0) I want to add an image and text.
If it is the second row I would like to add a button.
For all other rows I would like to just add a line of text.
I tried using indexPath.row. It worked the first time but when I scroll off of the screen and then back up my first image dissapears.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PromoListItem *promo = [promoList objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"Cell";
AsyncImageView *asyncImageView = nil;
UILabel *label = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(indexPath.row==0)
{
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGRect frame;
frame.origin.x = 0;
frame.origin.y = 0;
frame.size.width = 100;
frame.size.height = 100;
asyncImageView = [[[AsyncImageView alloc] initWithFrame:frame] autorelease];
asyncImageView.tag = ASYNC_IMAGE_TAG;
[cell.contentView addSubview:asyncImageView];
frame.origin.x = 110;
frame.size.width =100;
label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.tag = LABEL_TAG;
[cell.contentView addSubview:label];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else {
asyncImageView = (AsyncImageView *) [cell.contentView viewWithTag:ASYNC_IMAGE_TAG];
label = (UILabel *) [cell.contentView viewWithTag:LABEL_TAG];
}
NSURL *url = [NSURL URLWithString:promo.imgurl];
[asyncImageView loadImageFromURL:url];
label.text = promo.artistname;
}else
{
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGRect frame;
frame.origin.x = 0;
frame.origin.y = 0;
frame.size.width = 100;
frame.size.height = 100;
//asyncImageView = [[[AsyncImageView alloc] initWithFrame:frame] autorelease];
// asyncImageView.tag = ASYNC_IMAGE_TAG;
// [cell.contentView addSubview:asyncImageView];
frame.origin.x = 110;
frame.size.width =100;
label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.tag = LABEL_TAG;
[cell.contentView addSubview:label];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else {
// asyncImageView = (AsyncImageView *) [cell.contentView viewWithTag:ASYNC_IMAGE_TAG];
label = (UILabel *) [cell.contentView viewWithTag:LABEL_TAG];
}
//NSURL *url = [NSURL URLWithString:promo.imgurl];
//[asyncImageView loadImageFromURL:url];
label.text = promo.artistname;
}
return cell;
}
How customized do you want these cells? If you are only adding text, images and accessory buttons, you can create the cell outside of your row check, and then add the needed items inside those if statements.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
NSUInteger row = [indexPath row];
switch(row) {
case 0:
cell.textLabel.text = #"row 0";
cell.image
break;
case 1:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
default:
cell.textLabel.text = [NSString stringWithFormat:#"row %d",row];
break;
}
return cell;
However, the row will change when you start to recycle your cells, you may want to have something in the actual data that indicates how the cell should behave.