UITableViewCell not showing UILabel within - iphone

I have this code and it gives the result of the screenshot below. It's in the tableView:cellForRowAtIndexPath: method. I don't know why the text above the bars isn't showing. Does anyone have any ideas? Thank you.
cell.textLabel.text = #"";
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textAlignment = UITextAlignmentLeft;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UILabel * label = [[UILabel alloc] initWithFrame: cell.textLabel.frame];
label.backgroundColor = [UIColor clearColor];
label.text = [NSString stringWithFormat:#"%.1f%% (%i)",([[options_votes objectAtIndex: [indexPath row]] intValue] * 100.0 / total_votes),[[options_votes objectAtIndex: [indexPath row]] intValue]];
NSLog(#"%#",[NSString stringWithFormat:#"%.1f%% (%i)",([[options_votes objectAtIndex: [indexPath row]] intValue] * 100.0 / total_votes),[[options_votes objectAtIndex: [indexPath row]] intValue]]);
label.textAlignment = UITextAlignmentRight;
[cell.contentView addSubview:label];
[label release];
label = [[UILabel alloc] initWithFrame: cell.textLabel.frame];
label.backgroundColor = [UIColor clearColor];
label.text = [options objectAtIndex:[indexPath row]];
NSLog(#"%#",[options objectAtIndex:[indexPath row]]);
label.textAlignment = UITextAlignmentLeft;
[cell.contentView addSubview:label];
[label release];
UIImageView * image_view = [[UIImageView alloc] initWithImage:nav_delegate->back_bar];
image_view.frame = CGRectMake(10, 44, 280, 10);
[cell.contentView addSubview:image_view];
[image_view release];
image_view = [[UIImageView alloc] initWithImage:nav_delegate->blue_bar];
image_view.frame = CGRectMake(10, 44, 5 + [[options_votes objectAtIndex: [indexPath row]] intValue] * 275.0/total_votes, 10);
[cell.contentView addSubview:image_view];
[image_view release];

Just a stab in the dark, but because you did cell.textLabel.text = #"";, it might have shrank the frame of the textLabel there to a width of 0. Try creating your labels based off of a frame that you know to be of a correct visible size.

To show your 2 labels, you can use this code:
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (indexPath.row == 0) {
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(11, 10, 280, 25)];
label1.backgroundColor = [UIColor clearColor];
label1.text = #"Your Text Here";
label1.textAlignment = UITextAlignmentRight;
[cell.contentView addSubview:label1];
[label1 release];
}
if (indexPath.row == 1) {
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(11, 10, 280, 25)];
label2.backgroundColor = [UIColor clearColor];
label2.text = #"Your Text Here";
label2.textAlignment = UITextAlignmentLeft;
[cell.contentView addSubview:label2];
[label2 release];
}
if (indexPath.row == 2) {
// Put your imageView code here.
}
Note: if you are using a tableViewCell taller than defaults size, 44.0, you can change the 25 number in the initWithFrame method of the labels.
Hope this can help you :)

Related

How to display UILabel when UIImage is not present

I have a UITableView where I am loading images from the sever. But sometimes there are no images to display on the UITableView and at that I want to display UILabel. Wondering how would I accomplish this. I would appreciate any help or code snippets to achieve this.
Thank you very much!
I tried what you said. Everything works fine for the first time when you load the table, but as soon as you start scrolling all the labels and button go all over the places.
Here is my code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (msgImgFile){
NSLog (#"Image file found!");
lblOne = [[UILabel alloc] initWithFrame:CGRectMake(10, 360, 200, 20)];
lblTwo = [[UILabel alloc] initWithFrame:CGRectMake(10, 378, 150, 20)];
lblThree = [[UILabel alloc] initWithFrame:CGRectMake(10, 398, 150, 20)];
btnPlayStop.frame = CGRectMake(255.0f, 375.0f, 30.0f, 30.0f);
}
else
{
NSLog(#"Image file not found. Simply load the UILabel and UIButton");
lblOne = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 200, 20)];
lblTwo = [[UILabel alloc] initWithFrame:CGRectMake(10, 68, 150, 20)];
lblThree = [[UILabel alloc] initWithFrame:CGRectMake(10, 88, 150, 20)];
btnPlayStop.frame = CGRectMake(255.0f, 45.0f, 30.0f, 30.0f);
}
lblOne.font = [UIFont fontWithName:#"Arial" size:12];
[lblOne setBackgroundColor:[UIColor clearColor]];
lblOne.tag = 1;
lblTwo.font = [UIFont fontWithName:#"Arial" size:12];
[lblTwo setBackgroundColor:[UIColor clearColor]];
lblTwo.tag = 2;
lblThree.font = [UIFont fontWithName:#"Arial" size:10];
[lblThree setBackgroundColor:[UIColor clearColor]];
lblThree.tag = 3;
lblFour = [[UILabel alloc] initWithFrame:CGRectMake(10, 24, 150, 20)];
lblFour.font = [UIFont fontWithName:#"Arial" size:12];
[lblFour setBackgroundColor:[UIColor clearColor]];
lblFour.tag = 4;
btnPlayStop = [UIButton buttonWithType:UIButtonTypeCustom];
[btnPlayStop setTitle:#"Play" forState:UIControlStateNormal];
[btnPlayStop setImage:[UIImage imageNamed:#"Play Button.png"] forState:UIControlStateNormal];
[btnPlayStop addTarget:self action:#selector(playRecordClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:lblOne];
[cell addSubview:lblTwo];
[cell addSubview:lblThree];
[cell addSubview:lblFour];
[cell.contentView addSubview:btnPlayStop];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(queue, ^{
msgObjImg = (PFObject *)[self.imageDataMutArray objectAtIndex:indexPath.row];
createdDt = msgObjImg.createdAt;
msgImgFile = [msgObjImg objectForKey:#"siqImage"];
NSData *imgData = [msgImgFile getData];
UIImage *msgImgFound = [UIImage imageWithData:imgData];
UIImage *newImg = [self scaleImage:msgImgFound toSize:CGSizeMake(280.0, 300.0)];
dispatch_sync(dispatch_get_main_queue(), ^{
UILabel *dtTimeLabel = (UILabel *)[cell viewWithTag:3];
NSDateFormatter *dtFormat = [[NSDateFormatter alloc]init];
[dtFormat setDateFormat:#"MM-dd-yyyy HH:mm"];
[dtFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:-18000]];
NSString *createdDtString = [dtFormat stringFromDate:createdDt];
dtTimeLabel.text = [NSString stringWithFormat:#"Received on: %#",createdDtString];
[[cell imageView] setImage:newImg];
[cell setNeedsLayout];
}
return cell;
}
I can see where the problem is, and can tell you the right procedure to solve this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
lblOne = [[UILabel alloc]init];
lblOne.tag = 1;
lblTwo = [[UILabel alloc]init];
lblTwo.tag = 2;
lblThree = [[UILabel alloc]init];
lblThree.tag = 3;
lblFour = [[UILabel alloc]init];
lblFour.tag = 4;
btnPlayStop = [[UILabel alloc]init];
btnPlayStop.tag = 5;
// Perform addition functions ( your requirements )
[cell.contentView addSubview:lblOne];
[cell.contentView addSubview:lblTwo];
[cell.contentView addSubview:lblThree];
[cell.contentView addSubview:lblFour];
[cell.contentView addSubview:btnPlayStop];
}
else
{
lblOne = (UILabel*)[cell.contentView viewWithTag:1];
lblTwo = (UILabel*)[cell.contentView viewWithTag:2];
lblThree = (UILabel*)[cell.contentView viewWithTag:3];
lblFour = (UILabel*)[cell.contentView viewWithTag:4];
btnPlayStop = (UILabel*)[cell.contentView viewWithTag:5];
// AND SO ON ...
}
// SET THE VALUES HERE FOR YOUR CONTENT VALUES
return cell;
}
You need to create the cells using dynamic content views.
Try the above snippet, and modify according to your own..
In your cellForRowAtIndexPath method
UIImage *image = [imagesArray objectAtIndex:indexPath.row];
if (image) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:yourImageViewFrame];//create desired Frame
imageView.image = image;
[cell addSubview:imageView];
} else {
UILabel *label = [[UILabel alloc] initWithFrame:yourLabelFrame];//create desired frame
label.text = #"No Image";
[cell addSubview:label];
}
When your images are finished loading from the server, call [tableView reloadData];

When TableView scroll than value changed

I have created a table view that display data from array.but now when i scroll table view than my data change unacceptably.
So how can i prevent tableview to refresh every time when scroll and set all the data first time when table view created.
here is my code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
ExampleAppDataObject* theDataObject = [self theAppDataObject]; // Return the number of sections.
return theDataObject.vendorTableAry.count; //this is return value of array and it will be more than 2 which help to create section
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 4; // i want only 4 row in each section
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ExampleAppDataObject* theDataObject = [self theAppDataObject];
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell * cell = [tableView
dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
/*cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:SimpleTableIdentifier] autorelease];
*/
if (indexPath.row==0) {
UILabel * vendorLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, 60, 39)];
[[vendorLbl layer] setCornerRadius:5.0f];
//[vendorLbl setFont:[UIFont fontWithName:#"Helvetica-Bold" size:13.0]];
vendorLbl.backgroundColor = [UIColor clearColor];
[vendorLbl setText:#"vender"];
[cell.contentView addSubview:vendorLbl];
UILabel * columnLbl = [[UILabel alloc] initWithFrame:CGRectMake(80, 2, 10, 39)];
columnLbl.backgroundColor = [UIColor clearColor];
[columnLbl setText:#":"];
[cell.contentView addSubview:columnLbl];
UILabel * vendorValueLbl = [[UILabel alloc] initWithFrame:CGRectMake(98, 2, 195, 39)];
[[vendorValueLbl layer] setCornerRadius:5.0f];
//[vendorLbl setFont:[UIFont fontWithName:#"Helvetica-Bold" size:13.0]];
vendorValueLbl.backgroundColor = [UIColor clearColor];
[vendorValueLbl setText:[theDataObject.vendorTableAry objectAtIndex:indexPath.section]];
[cell.contentView addSubview:vendorValueLbl];
}
if (indexPath.row==1) {
UILabel * vendorLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, 60, 39)];
[[vendorLbl layer] setCornerRadius:5.0f];
//[vendorLbl setFont:[UIFont fontWithName:#"Helvetica-Bold" size:13.0]];
vendorLbl.backgroundColor = [UIColor clearColor];
[vendorLbl setText:#"Gallons"];
[cell.contentView addSubview:vendorLbl];
UILabel * columnLbl = [[UILabel alloc] initWithFrame:CGRectMake(80, 2, 10, 39)];
columnLbl.backgroundColor = [UIColor clearColor];
[columnLbl setText:#":"];
[cell.contentView addSubview:columnLbl];
UILabel * vendorValueLbl = [[UILabel alloc] initWithFrame:CGRectMake(98, 2, 195, 39)];
[[vendorValueLbl layer] setCornerRadius:5.0f];
//[vendorLbl setFont:[UIFont fontWithName:#"Helvetica-Bold" size:13.0]];
vendorValueLbl.backgroundColor = [UIColor clearColor];
[vendorValueLbl setText:[theDataObject.gallonsTableAry objectAtIndex:indexPath.section]];
[cell.contentView addSubview:vendorValueLbl];
}
if (indexPath.row==2) {
UILabel * vendorLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, 60, 39)];
[[vendorLbl layer] setCornerRadius:5.0f];
//[vendorLbl setFont:[UIFont fontWithName:#"Helvetica-Bold" size:13.0]];
vendorLbl.backgroundColor = [UIColor clearColor];
[vendorLbl setText:#"Route"];
[cell.contentView addSubview:vendorLbl];
UILabel * columnLbl = [[UILabel alloc] initWithFrame:CGRectMake(80, 2, 10, 39)];
columnLbl.backgroundColor = [UIColor clearColor];
[columnLbl setText:#":"];
[cell.contentView addSubview:columnLbl];
UILabel * vendorValueLbl = [[UILabel alloc] initWithFrame:CGRectMake(98, 2, 195, 39)];
[[vendorValueLbl layer] setCornerRadius:5.0f];
//[vendorLbl setFont:[UIFont fontWithName:#"Helvetica-Bold" size:13.0]];
vendorValueLbl.backgroundColor = [UIColor clearColor];
[vendorValueLbl setText:[theDataObject.routeTableAry objectAtIndex:indexPath.section]];
[cell.contentView addSubview:vendorValueLbl];
}
if (indexPath.row==3) {
UILabel * vendorLbl = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, 60, 39)];
[[vendorLbl layer] setCornerRadius:5.0f];
//[vendorLbl setFont:[UIFont fontWithName:#"Helvetica-Bold" size:13.0]];
vendorLbl.backgroundColor = [UIColor clearColor];
[vendorLbl setText:#"Date"];
[cell.contentView addSubview:vendorLbl];
UILabel * columnLbl = [[UILabel alloc] initWithFrame:CGRectMake(80, 2, 10, 39)];
columnLbl.backgroundColor = [UIColor clearColor];
[columnLbl setText:#":"];
[cell.contentView addSubview:columnLbl];
UILabel * vendorValueLbl = [[UILabel alloc] initWithFrame:CGRectMake(98, 2, 195, 39)];
[[vendorValueLbl layer] setCornerRadius:5.0f];
//[vendorLbl setFont:[UIFont fontWithName:#"Helvetica-Bold" size:13.0]];
vendorValueLbl.backgroundColor = [UIColor clearColor];
[vendorValueLbl setText:[theDataObject.dateTableAry objectAtIndex:indexPath.section]];
[cell.contentView addSubview:vendorValueLbl];
}
}
return cell;
}
Just set dequeueReusableCellWithIdentifier to nil like below..
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: nil];

xml data in UITableview in iPhone app

how can i show the xml data in UITableView according to the attribute field of the xml data?
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *) string {
[currentElementValue appendString:string];
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *) elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
NSLog(#"elementName %# %#", elementName, currentElementValue);
currentElementValue=nil;
[currentElementValue release];
}
here is some code where i am getting the element name in my xml file.now i want to display these data on a UITableView cell.
if i become unable to express the problem then please knock....
NOTE THAT: if somebody give me source code then it will better for me,because its very urgent for me.
Thanks in Advance.
Every time you parse your xml data, get that value intoone array and than display that in tableview.
#pragma mark tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
int sectionCount = [records count];
NSLog(#"section cout: %d",sectionCount);
return sectionCount;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:15.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"arrow.png"]];
cell.accessoryView = imageView;
cell.accessoryType = UITableViewCellSelectionStyleNone;
tableView.separatorColor = [UIColor clearColor];
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
cellView = [[[UIView alloc] initWithFrame:CGRectMake(5,8,290, 120)] autorelease];
cellView.backgroundColor = [UIColor clearColor];
cellView.tag =10;
[cell.contentView addSubview:cellView];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(2, 40, 48, 48)];
imgView.image = [UIImage imageNamed:#"productbox.png"];
imgView.layer.borderColor = [UIColor blackColor].CGColor;
imgView.layer.borderWidth = 2.0;
imgView.tag = 5;
[cellView addSubview:imgView];
CGRect idLabelRect = CGRectMake(65, 0, 190, 18);
idLabel = [[[UILabel alloc] initWithFrame:idLabelRect] autorelease];
idLabel.textAlignment = UITextAlignmentLeft;
idLabel.textColor = [UIColor blackColor];
idLabel.font = [UIFont systemFontOfSize:12];
idLabel.backgroundColor = [UIColor clearColor];
idLabel.layer.borderColor = [UIColor grayColor].CGColor;
idLabel.tag = 0;
CGRect statusRect = CGRectMake(65, 22, 190, 22);
statusLabel = [[[UILabel alloc] initWithFrame:statusRect] autorelease];
statusLabel.textAlignment = UITextAlignmentLeft;
statusLabel.textColor = [UIColor blackColor];
statusLabel.font = [UIFont systemFontOfSize:12];
statusLabel.backgroundColor = [UIColor clearColor];
statusLabel.layer.borderColor = [UIColor grayColor].CGColor;
statusLabel.tag = 1;
CGRect orderDateRect = CGRectMake(65, 48, 190, 22);
orderDate = [[[UILabel alloc] initWithFrame:orderDateRect] autorelease];
orderDate.textAlignment = UITextAlignmentLeft;
orderDate.textColor = [UIColor blackColor];
orderDate.font = [UIFont systemFontOfSize:12];
orderDate.backgroundColor = [UIColor clearColor];
orderDate.layer.borderColor = [UIColor grayColor].CGColor;
orderDate.tag = 2;
CGRect byRect = CGRectMake(65, 75, 190, 22);
byLabel = [[[UILabel alloc] initWithFrame:byRect] autorelease];
byLabel.textAlignment = UITextAlignmentLeft;
byLabel.textColor = [UIColor blackColor];
byLabel.font = [UIFont systemFontOfSize:12];
byLabel.backgroundColor = [UIColor clearColor];
byLabel.layer.borderColor = [UIColor grayColor].CGColor;
byLabel.tag = 3;
CGRect totalRect = CGRectMake(65, 98, 190, 22);
totalLabel = [[[UILabel alloc] initWithFrame:totalRect] autorelease];
totalLabel.textAlignment = UITextAlignmentLeft;
totalLabel.textColor = [UIColor blackColor];
totalLabel.font = [UIFont systemFontOfSize:12];
totalLabel.backgroundColor = [UIColor clearColor];
totalLabel.layer.borderColor = [UIColor grayColor].CGColor;
totalLabel.tag = 4;
[cellView addSubview:idLabel];
[cellView addSubview:statusLabel];
[cellView addSubview:orderDate];
[cellView addSubview:byLabel];
[cellView addSubview:totalLabel];
}
cellView = (UIView *)[cell.contentView viewWithTag:10];
idLabel = (UILabel *)[cellView viewWithTag:0];
statusLabel = (UILabel *)[cellView viewWithTag:1];
orderDate = (UILabel *)[cellView viewWithTag:2];
byLabel = (UILabel *)[cellView viewWithTag:3];
totalLabel = (UILabel *)[cellView viewWithTag:4];
imgView = (UIImageView *)[cellView viewWithTag:5];
idLabel.text = [NSString stringWithFormat:#"Order Id: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:0]];
statusLabel.text = [NSString stringWithFormat:#"Status: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:1]];
orderDate.text = [NSString stringWithFormat:#"Date: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:2]];
byLabel.text =[NSString stringWithFormat:#"By: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:3]];
totalLabel.text =[NSString stringWithFormat:#"Total: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:4]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Get the selected country
NSString *selectedSection = [ NSString stringWithFormat:#"%#",[[records objectAtIndex:indexPath.section] objectAtIndex:0] ];
dvController = [[OrderDetailsViewController alloc] initWithNibNameAndCurrentTweetUser:#"OrderDetailsViewController" bundle:nil:selectedSection];
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 130;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)];
[v setBackgroundColor:[UIColor blackColor]];
UILabel* hdr = [[[UILabel alloc] initWithFrame:CGRectMake(10,0, tableView.bounds.size.width,20)] autorelease];
hdr.textAlignment = UITextAlignmentLeft;
hdr.font = [UIFont fontWithName:#"Arial-BoldMT" size:12];
hdr.textColor = [UIColor whiteColor];
hdr.backgroundColor = [UIColor blackColor];
[v addSubview:hdr];
hdr.text = [NSString stringWithFormat:#"Order #%#",[[records objectAtIndex:section] objectAtIndex:0]];
return v ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 20;
}

Add textfield and checkbox on tableview

In my Iphone app I have to add textfield at two rows of tableview and a checkbox to one row. Can anyone provide me an example that can help me. I am new on Iphone and I don't know how to start with this.
Thanks in advance..
Hey try this one in your cellForRowAtindexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:#"CellID%d%d",indexPath.section,indexPath.row]];
if (!cell){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:[NSString stringWithFormat:#"CellID%d%d",indexPath.section,indexPath.row]] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if(indexPath.row == 0){
// NSString *daytime = [[NSUserDefaults standardUserDefaults] objectForKey:#"day"];
UITextField *text0 = [[UITextField alloc] initWithFrame:CGRectMake(20, 12, 120, 22)];
text0.textAlignment = UITextAlignmentLeft;
text0.backgroundColor = [UIColor whiteColor];
text0.borderStyle = UITextBorderStyleNone;
text0.userInteractionEnabled = FALSE;
text0.font = [UIFont boldSystemFontOfSize:17];
text0.textColor = [UIColor blackColor];
text0.delegate = self;
text0.text = #"Type de Parteneria";
[cell addSubview:text0];
UITextField *text1 = [[UITextField alloc] initWithFrame:CGRectMake(120, 12, 160, 20)];
text1.textAlignment = UITextAlignmentRight;
text1.borderStyle = UITextBorderStyleNone;
text1.backgroundColor = [UIColor whiteColor];
text1.userInteractionEnabled = TRUE;
text1.font = [UIFont boldSystemFontOfSize:16];
text1.textColor = [UIColor colorWithRed:114.0f/255.0f green:136.0f/255.0f blue:165.0f/255.0f alpha:1.0];
text1.delegate = self;
[cell addSubview:text1];
}
else if(indexPath.row == 1){
// NSString *daytime = [[NSUserDefaults standardUserDefaults] objectForKey:#"day"];
UITextField *text0 = [[UITextField alloc] initWithFrame:CGRectMake(20, 12, 120, 22)];
text0.textAlignment = UITextAlignmentLeft;
text0.backgroundColor = [UIColor whiteColor];
text0.borderStyle = UITextBorderStyleNone;
text0.userInteractionEnabled = FALSE;
text0.font = [UIFont boldSystemFontOfSize:17];
text0.textColor = [UIColor blackColor];
text0.delegate = self;
text0.text = #"Audi R8";
[cell addSubview:text0];
UIButton *signBtn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
signBtn.frame = CGRectMake(15, 170, 290, 35);
[signBtn setTitle:#"Sign Up" forState:UIControlStateNormal];
[signBtn setBackgroundImage:[UIImage imageNamed:#"CheckBox.png"] forState:UIControlStateNormal];
[signBtn setFont:[UIFont boldSystemFontOfSize:17]];
[signBtn addTarget:self action:#selector(checkPressed) forControlEvents:UIControlEventTouchDown];
[cell addSubview:signBtn];
}
return cell;
}
If you get problem than message me
as per your comment try this :
if(indexPath.row==1)
{
UITextField *labl=[[UITextField alloc] init];
// where is your text field's frame ????
labl.frame = CGRectMake(10,10,50,50);
labl.text=#"data";
[cell addSubview:labl];
}

how to give different header section title for different sections

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(20, 6, 300, 30);
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithHue:(136.0/360.0) // Slightly bluish green
saturation:1.0
brightness:0.60
alpha:1.0];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:16];
label.text = sectionTitle;
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)];
[view autorelease];
[view addSubview:label];
return view;
}
this is for one section if i have 9 section then how can i set title
You need to implement
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
delegate method for UITableView.
This might solve your problem:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIControl *containerView = [[[UIControl alloc] initWithFrame:CGRectMake(0, 0, 320, 20)] autorelease];
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 20)] autorelease];
headerLabel.textColor = [UIColor colorWithRed:50.0/255.0 green:44.0/255.0 blue:37.0/255.0 alpha:1.0];
[headerLabel setFont:[UIFont boldSystemFontOfSize:11.0]];
//[headerLabel setFont:[UIFont boldSystemFontOfSize:13.0]];
headerLabel.backgroundColor = [UIColor clearColor];
if(section == 0)
headerLabel.text = [NSString stringWithFormat:#"First Section"];
if(section == 1)
headerLabel.text = [NSString stringWithFormat:#"Second Section"];
if(section == 2)
headerLabel.text = [NSString stringWithFormat:#"Third Section"];
headerLabel.textColor = [UIColor colorWithRed:78.0/255.0 green:70.0/255.0 blue:59.0/255.0 alpha:1.0];
//headerLabel.font = [UIFont boldSystemFontOfSize:13.0];
[containerView addSubview:headerLabel];
return containerView;
}
Try this!
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *v =[[[UIView alloc] init] autorelease];
v.backgroundColor = [UIColor clearColor];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 7, tableView.bounds.size.width - 10, 18)] autorelease];
label.textColor = [UIColor whiteColor];
label.font = [UIFont boldSystemFontOfSize:15];
label.backgroundColor = [UIColor clearColor];
if (section == 1) {
label.text = #"Section 1";
[v addSubview:label];
}
else if (section == 2) {
label.text = #"Section 2";
[v addSubview:label];
}
else if (section == 3) {
label.text = #"Section 3";
[v addSubview:label];
}
return v;
}
You need to know the titles, just use an NSArray, make it global alloc and init it in viewDidLoad and release the array in dealloc.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *aView=[[[NSBundle mainBundle] loadNibNamed:#"tblHeader" owner:self options:nil]objectAtIndex:0];
[(UILabel*)[aView viewWithTag:1] setText:[[self.reports objectAtIndex:section] valueForKey:#"domain"]];
return aView;
return nil;
}
Here u can insert your custom cell also u can use the Section and get those section wise you can use switch case and easily access the section.