The UITableView cells are repeating on scroll and overlapping each other.
How can i fix this issue
here is my code
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 11;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
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)
{
backImage = [[UIImageView alloc] initWithFrame:CGRectMake(2, 4, 316, 62)];
[buyBtn setFrame:CGRectMake(257, 35, 57, 24)];
img = [[UIImageView alloc] initWithFrame:CGRectMake(257, 10, 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"]];
}
else
{
backImage = [[UIImageView alloc] initWithFrame:CGRectMake(2, 4, 316, 62)];
[buyBtn setFrame:CGRectMake(257, 35, 57, 24)];
img = [[UIImageView alloc] initWithFrame:CGRectMake(257, 10, 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];
static NSString *myIdentifier = #"myIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];
NSLog(#"%d" , [[cell subviews] count]);
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryNone;
[cell setSelectionStyle:UITableViewCellEditingStyleNone];
cell.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:backImage];
[cell.contentView addSubview:detailTextLabel];
[cell.contentView addSubview:textLabel];
[cell.contentView addSubview:buyBtn];
[cell.contentView addSubview:img];
[cell.contentView addSubview:lbl];
return cell;
}
Try this code :-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
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)
{
backImage = [[UIImageView alloc] initWithFrame:CGRectMake(2, 4, 316, 62)];
[buyBtn setFrame:CGRectMake(257, 35, 57, 24)];
img = [[UIImageView alloc] initWithFrame:CGRectMake(257, 10, 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"]];
}
else
{
backImage = [[UIImageView alloc] initWithFrame:CGRectMake(2, 4, 316, 62)];
[buyBtn setFrame:CGRectMake(257, 35, 57, 24)];
img = [[UIImageView alloc] initWithFrame:CGRectMake(257, 10, 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];
static NSString *myIdentifier = #"myIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];
NSLog(#"%d" , [[cell subviews] count]);
//if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryNone;
[cell setSelectionStyle:UITableViewCellEditingStyleNone];
cell.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:backImage];
[cell.contentView addSubview:detailTextLabel];
[cell.contentView addSubview:textLabel];
[cell.contentView addSubview:buyBtn];
[cell.contentView addSubview:img];
[cell.contentView addSubview:lbl];
return cell;
}
You need to check height for cell in
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
It should be greater then max height used in your code may be 65
Don't use reuseIdentifier and set to nil.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
}
What is happening here is that you use
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];
This code makes sure that when you are scrolling in a UITableView, the cell gets reused to save on memory. Since you are using the same identifier for all cells, it will just reuse a cell that already has a 'backImage', 'detailTextLabel' and so forth, and add the new view components on top of the already existing ones.
You could fix this by using a code similar to this:
static NSString *myIdentifier = #"myIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myIdentifier];
// Create backImage, detailTextLabel, etc. here
...
// Give view components tags
backImage.tag = 1;
detailTextLabel.tag = 2;
...
}
UIImageView* retrievedBackImage = (UIImageView*)[cell.contentView viewWithTag:1];
UILabel* retrievedDetailTextLabel = (UILabel *)[cell.contentView viewWithTag:2];
...
// Initialise values
retrievedBackImage.image = [UIImage imageNamed:#"someImage.png"];
retrievedDetailTextLabel.text = #"something";
...
Hope this was helpful.
Nicolas
Related
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];
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];
I have a problem creating the UITableView, because I can't see in one of the cells the label and the stepper, that i assigned to it.
Here is the code, I hope someone can figure the problem out. When it arrives to case 4, it doesn't show the lblVarsta in the cell, only the stepper. I debugged it and the value is there, but the label with that value can't be seen. Thank you!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UITextField *inputField;
lblRasa = [[UILabel alloc]initWithFrame:CGRectMake(120, 12, 185, 30)];
lblCuloare = [[UILabel alloc]initWithFrame:CGRectMake(120, 12, 185, 30)];
lblJudet = [[UILabel alloc]initWithFrame:CGRectMake(120, 12, 185, 30)];
lbldate = [[UILabel alloc] initWithFrame:CGRectMake(120, 12, 185, 30)];
inputField = [[UITextField alloc]initWithFrame:CGRectMake(120, 12, 185, 30)];
inputField.adjustsFontSizeToFitWidth = YES;
inputField.borderStyle = UITextBorderStyleRoundedRect;
inputField.tag = indexPath.row;
inputField.delegate = self;
segmentedControl1 = [[UISegmentedControl alloc] initWithFrame:CGRectMake(220, 10, 100, 10)];
segmentedControl2 = [[UISegmentedControl alloc] initWithFrame:CGRectMake(220, 10, 100, 10)];
NSArray * segmentItems = [NSArray arrayWithObjects: #"Da", #"Nu", nil];
// UIDatePicker *pv = [[UIDatePicker alloc] init];
// UIActionSheet *uas = [[UIActionSheet alloc] initWithTitle:#"pv" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Select", nil];
//
// [uas addSubview:pv];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellAccessoryNone reuseIdentifier:CellIdentifier];
}
switch ([indexPath row])
{
case 0:
cell.textLabel.text = #"Nume";
inputField.text = dog?dog.title:#"";
[cell addSubview:inputField];
break;
case 1:
cell.textLabel.text = #"Rasa";
lblRasa.text = dog?dog.rasa:#"";
[cell addSubview:lblRasa];
break;
case 2:
cell.textLabel.text = #"Culoare";
lblCuloare.text = dog?dog.culoare:#"";
[cell addSubview:lblCuloare];
break;
case 3:{
cell.textLabel.text = #"Data nasterii";
lbldate.text = dog?dog.data_nas:#"";
[cell addSubview:lbldate];
[cell addSubview:actionSheet];
break;}
case 4:{
lblVarsta = [[UILabel alloc] initWithFrame:CGRectMake(120, 12, 185, 30)];
stepper = [[UIStepper alloc] initWithFrame:CGRectMake(220, 10, 100, 10)];
cell.textLabel.text = #"Varsta";
//inputField.text = dog?[NSString stringWithFormat:#"%d",dog.varsta]:#"";
lblVarsta.text = #"3 ani";
[stepper addTarget:self action:#selector(stepperValueChanged:) forControlEvents:UIControlEventValueChanged];
[lblVarsta setTextColor:[UIColor whiteColor]];
[lblVarsta setBackgroundColor:[UIColor clearColor]];
//[lbl5 setTextAlignment:UITextAlignmentLeft];
[stepper setStepValue:10];
[cell addSubview:lblVarsta];
[cell.contentView addSubview:stepper];
break;}
case 5:
cell.textLabel.text = #"Proprietar";
inputField.text = dog?dog.proprietar:#"";
[cell addSubview:inputField];
break;
case 6:
cell.textLabel.text = #"Judet";
lblJudet.text = dog?dog.judet:#"";
[cell addSubview:lblJudet];
break;
case 7:
cell.textLabel.text = #"Adresa";
inputField.text = dog?dog.adresa:#"";
[cell addSubview:inputField];
break;
case 8:{
cell.textLabel.text = #"Parinti";
//inputField.text = dog?dog.parinti:#"";
//lblVarsta.text = [NSString stringWithFormat:#"%.f", stepper.value];
segmentedControl1 = [[UISegmentedControl alloc] initWithItems: segmentItems];
// segmentedControl1.segmentedControlStyle = UISegmentedControlStyleBar;
// segmentedControl1.selectedSegmentIndex = 0;
[segmentedControl1 addTarget:self action:#selector(segmentedControlIndexChanged:) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:segmentedControl1];
break;}
case 9:
cell.textLabel.text = #"Pedigree";
segmentedControl1 = [[UISegmentedControl alloc] initWithItems: segmentItems];
[segmentedControl1 addTarget:self action:#selector(segmentedControlIndexChanged:) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:segmentedControl2];
break;
}
return cell;
}
You have
[cell addSubview:lblVarsta];
[cell.contentView addSubview:stepper];
and
lblVarsta = [[UILabel alloc] initWithFrame:CGRectMake(120, 12, 185, 30)];
stepper = [[UIStepper alloc] initWithFrame:CGRectMake(220, 10, 100, 10)];
Why dont you try
[cell.contentView addSubview:stepper];
[cell.contentView addSubview:lblgarsta];
with the frames as follows
lblVarsta = [[UILabel alloc] initWithFrame:CGRectMake(120, 12, 100, 30)];
stepper = [[UIStepper alloc] initWithFrame:CGRectMake(220, 10, 100, 10)];
You have to add both the subviews to the contentview and their frames have to be such that both of them can appear.
I have a global declared array and loading that array in table view but when i go to other controller and update that array and reload table view again then text in labels in table view cell is overriding. Newer update data is overriding and previous also displaying in same label. here is my code: Where item array is globally declared array.
- (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] autorelease];
}
UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(45, 8, 100, 25)];
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.font = [UIFont fontWithName:#"Arial" size:16];
nameLabel.text = [[itemsArray objectAtIndex:indexPath.row] objectForKey:#"itemname"];
[cell addSubview:nameLabel];
[nameLabel release];
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
deleteButton.frame= CGRectMake(3, 3, 30, 30);
[deleteButton setImage: [UIImage imageNamed:#"cross_btn.png"] forState:UIControlStateNormal];
[cell addSubview:deleteButton];
UILabel *priceMark = [[UILabel alloc]initWithFrame:CGRectMake(150, 8, 65, 25)];
priceMark.backgroundColor = [UIColor clearColor];
priceMark.font = [UIFont fontWithName:#"Arial" size:14];
priceMark.text = [NSString stringWithFormat:#"Rs: %#",[[itemsArray objectAtIndex:indexPath.row]
objectForKey:#"amount"]];
[cell addSubview:priceMark];
[priceMark release];
UILabel *qtyLabel = [[UILabel alloc]initWithFrame:CGRectMake(225, 8, 60, 25)];
qtyLabel.backgroundColor = [UIColor clearColor];
qtyLabel.font = [UIFont fontWithName:#"Arial" size:14];
qtyLabel.text = [[itemsArray objectAtIndex:indexPath.row] objectForKey:#"qty"];
[cell addSubview:qtyLabel];
[qtyLabel release];
return cell;
}
EDIT 2
now i am getting other problem when i update item array that is globally declared at 0 index and return back then it wont update the row in table view row at index 0 but when i update other rows then it updates. And also when i scroll table view towards upward then it shows value at index 0 on all index. here is my code
- (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];
nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(45, 8, 100, 25)];
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.font = [UIFont fontWithName:#"Arial" size:16];
[cell addSubview:nameLabel];
deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
deleteButton.frame= CGRectMake(3, 3, 30, 30);
[deleteButton setImage: [UIImage imageNamed:#"cross_btn.png"] forState:UIControlStateNormal];
[cell addSubview:deleteButton];
priceMark = [[UILabel alloc]initWithFrame:CGRectMake(200, 8, 60, 25)]; //150, 8, 65, 25
priceMark.backgroundColor = [UIColor clearColor];
priceMark.font = [UIFont fontWithName:#"Arial" size:14];
[cell addSubview:priceMark];
qtyLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 8, 65, 25)]; //225, 8, 60, 25
qtyLabel.backgroundColor = [UIColor clearColor];
qtyLabel.font = [UIFont fontWithName:#"Arial" size:14];
[cell addSubview:qtyLabel];
}
nameLabel.text = [[itemsArray objectAtIndex:indexPath.row] objectForKey:#"itemname"];
priceMark.text = [NSString stringWithFormat:#"Rs: %#",[[itemsArray
objectAtIndex:indexPath.row] objectForKey:#"amount"]];
qtyLabel.text = [[itemsArray objectAtIndex:indexPath.row] objectForKey:#"qty"];
return cell;
}
You should not create new UILabels and UIButtons and adding them as a subview every time.
Your problem here is that you add new ones every time.
You should make a custom UITableViewCell with labels and buttons and set them.
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];
}