previous text in label in tableview is displaying after reloading tableview - iphone

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.

Related

App crash when scroll on UiTableViewCell

Hi my App goes crash on scrolling tableview and mentain a crash log name with LowMemory.
Can anyone tell me how can i manage this. Code which i ma using is here :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
NSLog(#" %i",indexPath.section);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//cell=nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIImageView *cellBg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 768, 90)];
cellBg.image=[UIImage imageNamed:#"Cellbg.png"];
[cell addSubview:cellBg];
UILabel *lblRecipeBorder=[[UILabel alloc]initWithFrame:CGRectMake(105, 5, 640, 80)];
lblRecipeBorder.backgroundColor=[UIColor blackColor];
lblRecipeBorder.layer.borderWidth=5.0;
lblRecipeBorder.layer.cornerRadius=8.0;
lblRecipeBorder.layer.borderColor=[[UIColor darkGrayColor] CGColor];
[cell addSubview:lblRecipeBorder];
UIImageView *RecipeeImg=[[UIImageView alloc]initWithFrame:CGRectMake(20, 5, 100, 80)];
RecipeeImg.backgroundColor=[UIColor clearColor];
RecipeeImg.layer.borderWidth=5.0;
RecipeeImg.layer.cornerRadius=8.0;
RecipeeImg.layer.borderColor=[[UIColor darkGrayColor] CGColor];
RecipeeImg.image=[UIImage imageNamed:[[appdelegate.arrFiderTool objectAtIndex:indexPath.row] objectForKey:#"RecipestepPics"]];
RecipeeImg.layer.masksToBounds = YES;
[cell addSubview:RecipeeImg];
UILabel *lblRecipee=[[UILabel alloc]initWithFrame:CGRectMake(140, 10, 620, 70)];
lblRecipee.backgroundColor=[UIColor clearColor];
[lblRecipee setNumberOfLines:2];
lblRecipee.textColor=[UIColor colorWithRed:255.0/255.0 green:52.0/255.0 blue:179.0/255.0 alpha:1.0];
//lblRecipee.font=[UIFont boldSystemFontOfSize:18.0];
lblRecipee.font=[UIFont fontWithName:#"Noteworthy-Bold" size:18.0];
//[lblMeetingId setFont:[UIFont fontWithName:#"Times New Roman" size:14]];
lblRecipee.text=[[appdelegate.arrFiderTool objectAtIndex:indexPath.row] objectForKey:#"RecipeName"];
[cell addSubview:lblRecipee];
[RecipeeImg release];
[lblRecipee release];
[cellBg release];
[lblRecipeBorder release];
// }
cell.selectionStyle=UITableViewCellSelectionStyleNone;
return cell;
}
You should be reusing the labels and image views to add the cell. You can do this by subclassing the UITableViewCell or by giving the views tags. In my opinion subclassing is the best way, but for now I will you an example of the tagging views:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
NSLog(#" %i",indexPath.section);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UIImageView *recipeImageView = nil;
UILabel *recipeLabel = nil;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UIImageView *cellBg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 768, 90)];
cellBg.image = [UIImage imageNamed:#"Cellbg.png"];
[cell addSubview:cellBg];
UILabel *lblRecipeBorder=[[UILabel alloc]initWithFrame:CGRectMake(105, 5, 640, 80)];
lblRecipeBorder.backgroundColor=[UIColor blackColor];
lblRecipeBorder.layer.borderWidth=5.0;
lblRecipeBorder.layer.cornerRadius=8.0;
lblRecipeBorder.layer.borderColor=[[UIColor darkGrayColor] CGColor];
[cell addSubview:lblRecipeBorder];
UIImageView *recipeImageView =[[UIImageView alloc]initWithFrame:CGRectMake(20, 5, 100, 80)];
recipeImageView.backgroundColor=[UIColor clearColor];
recipeImageView.layer.borderWidth=5.0;
recipeImageView.layer.cornerRadius=8.0;
recipeImageView.layer.borderColor=[[UIColor darkGrayColor] CGColor];
recipeImageView.layer.masksToBounds = YES;
recipeImageView.tag = 456;
[cell addSubview:recipeImageView];
UILabel *recipeLabel=[[UILabel alloc]initWithFrame:CGRectMake(140, 10, 620, 70)];
recipeLabel.backgroundColor=[UIColor clearColor];
[recipeLabel setNumberOfLines:2];
recipeLabel.textColor=[UIColor colorWithRed:255.0/255.0 green:52.0/255.0 blue:179.0/255.0 alpha:1.0];
recipeLabel.font=[UIFont fontWithName:#"Noteworthy-Bold" size:18.0];
recipeLabel.tag = 457;
[cell recipeLabel];
[recipeImageView release];
[recipeLabel release];
[cellBg release];
[lblRecipeBorder release];
} else {
recipeImageView = [cell viewWithTag:456];
recipeLabel = [cell viewWithTag:457];
}
NSDictionary *item = [appdelegate.arrFiderTool objectAtIndex:indexPath.row];
recipeImageView.image = [UIImage imageNamed:[item objectForKey:#"RecipestepPics"]];
recipeLabel.text = [item objectForKey:#"RecipeName"];
return cell;
}
Change:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
To:
UITableViewCell *cell [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];

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];

Repeating and Overlapping UITableView cells in iPhone

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

UILabel in Table View - the right way?

I was trying to implement a UILabel into a cell and what I get is overlapping of some values when I scroll the table up and down a couple of times. I work with ARC so there is no release when I want, so my question is : What's the right way of implementing a Label into a tableView cell?
Here is how it looks
- (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];
}
// Configure the cell...
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1];
cell.textLabel.highlightedTextColor = [UIColor colorWithRed:0.753 green:0.651 blue:0.1569 alpha:1];
UILabel *cellLabelS1 = [[UILabel alloc] initWithFrame:CGRectMake(250, 0, cell.frame.size.width, cell.frame.size.height)];
cellLabelS1.backgroundColor = [UIColor clearColor];
cellLabelS1.font = [UIFont boldSystemFontOfSize:16];
[cellLabelS1 setTextColor:[UIColor whiteColor]];
[cellLabelS1 setText:temperatureString];
temperatureString = nil;
[cell addSubview:cellLabelS1];
[[cell imageView]setImage:[UIImage imageNamed:imageFromCodeDay1]];
[[cell textLabel]setText:cityString];
return cell;
}
You should add a label to the cell only if you don't have one.If you reuse the cells on the second pass you add it again.
So my advice is to set a tag to the label and try to see if the cell contentView bass already the label. If not create and add it.
UILabel *myLabel = (UILabel *)[cell.contentView viewWithTag:2002];
if(!myLabel){
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 22)];
myLabel.tag = 2002;
[cell.contentView addSubview:myLabel];
}
myLabel.text = #"my new text";
- (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];
UILabel *cellLabelS1 = [[UILabel alloc] initWithFrame:CGRectMake(250, 0,cell.frame.size.width, cell.frame.size.height)];
cellLabelS1.backgroundColor = [UIColor clearColor];
cellLabelS1.tag = 200;
cellLabelS1.font = [UIFont boldSystemFontOfSize:16];
[cellLabelS1 setTextColor:[UIColor whiteColor]];
[cell addSubview:cellLabelS1];
}
// Configure the cell...
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1];
cell.textLabel.highlightedTextColor = [UIColor colorWithRed:0.753 green:0.651 blue:0.1569 alpha:1];
UILabel *cellLabelS1 = (UILabel*)[cell viewWithTag:200];
[cellLabelS1 setText:temperatureString];
temperatureString = nil;
[[cell imageView]setImage:[UIImage imageNamed:imageFromCodeDay1]];
[[cell textLabel]setText:cityString];
return cell;
}
may this will help you....
Your problem is in these lines:
UILabel *cellLabelS1 = [[UILabel alloc] initWithFrame:CGRectMake(250, 0, cell.frame.size.width, cell.frame.size.height)];
cellLabelS1.backgroundColor = [UIColor clearColor];
cellLabelS1.font = [UIFont boldSystemFontOfSize:16];
[cellLabelS1 setTextColor:[UIColor whiteColor]];
When you get a reused cell back from the table view, it already hass this label in it. What you can do to avoid adding duplicate labels is add it only if you need to allocate a new cell. However, this can make retrieving the label back from a reused cell quite complicated.
I would personally suggest creating a custom UITableViewCell in interface builder, and creating a custom UITableViewCell subclass that has a UILabel property.

'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

my code is
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
/// labels - names of Cities ///
UILabel *lblCity = [[UILabel alloc]initWithFrame:CGRectMake(15, 00, 200, 22)];
lblCity.font = [UIFont systemFontOfSize:14];
lblCity.backgroundColor = [UIColor clearColor];
//lblCity.backgroundColor = [UIColor redColor];
UILabel *lblDate = [[UILabel alloc]initWithFrame:CGRectMake(200, 00, 200, 22)];
lblDate.font = [UIFont systemFontOfSize:14];
lblDate.backgroundColor = [UIColor clearColor];
//lblDate.backgroundColor = [UIColor redColor];
UILabel *lblSchool = [[UILabel alloc]initWithFrame:CGRectMake(350, 00, 400, 22)];
lblSchool.font = [UIFont systemFontOfSize:14];
lblSchool.backgroundColor = [UIColor clearColor];
//lblSchool.backgroundColor = [UIColor redColor];
/// Labels for description of city events ///
UILabel *lblEvent = [[UILabel alloc]initWithFrame:CGRectMake(15, 00, 200, 30)];
lblEvent.font = [UIFont systemFontOfSize:12];
lblEvent.backgroundColor = [UIColor clearColor];
UILabel *lblEventAtDate = [[UILabel alloc]initWithFrame:CGRectMake(200, 00, 200, 30)];
lblEventAtDate.font = [UIFont systemFontOfSize:12];
lblEventAtDate.backgroundColor = [UIColor clearColor];
UILabel *lblEventAtSchool = [[UILabel alloc]initWithFrame:CGRectMake(350, 00, 400, 30)];
lblEventAtSchool.font = [UIFont systemFontOfSize:12];
lblEventAtSchool.backgroundColor = [UIColor clearColor];
if(RequestType == 2)
{
UIImageView *imgEventLabel = [[UIImageView alloc]initWithFrame:CGRectMake(00, 00, 480, 22)];
UIView *viewDescription = [[UIView alloc]initWithFrame:CGRectMake(00, 00, 480, 35)];
if(indexPath.row == 0)
{
static NSString *CellIdentifier = #"Cell11";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
lblCity.text = #"City" ;
// [cell addSubview:lblCity];
lblDate.text = #"Date" ;
// [cell addSubview:lblDate];
lblSchool.text = #"School" ;
// [cell addSubview:lblSchool];
imgEventLabel.image = [UIImage imageNamed:#"city_date_place.png"];
// [cell addSubview:imgEventLabel];
[imgEventLabel addSubview:lblCity];
[imgEventLabel addSubview:lblDate];
[imgEventLabel addSubview:lblSchool];
[cell.contentView addSubview:imgEventLabel];
}
return cell;
}
if(indexPath.row == 1)
{
static NSString *CellIdentifier = #"Cell12";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.backgroundColor=[UIColor clearColor];
cell.textLabel.numberOfLines = 999;
lblEvent.text = #"Event in City";
lblEventAtDate.text = #"Event on Date";
lblEventAtSchool.text = #"Event at School";
[viewDescription addSubview:lblEvent];
[viewDescription addSubview:lblEventAtDate];
[viewDescription addSubview:lblEventAtSchool];
[cell.contentView addSubview:viewDescription];
}
return cell;
}
}
// Configure the cell...
return cell;
}
I don't know where is fault, Please Help.
After your first call of dequeueReusableCellWithIdentifier: You didn't check for nil and create a new one if there are no available cells in the reuse queue.
In other words, if there are no cells in the reuse queue, your request type is not equal to 2, and row is not equal to 0 or 1, your cell will not be created. This causes an exception you see in the console.
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
you don't need to write that on start as you create the cell according to conditions .
At the end just return nil. Because I think there must be some condition true when you execute your code
Sorry I do not have enough points to comment on your question. Have you tried using Custom Cells? Custom Cell is like a modification of the current TableViewCells. You can do almost anything you want to that the TableViewCells do not provide. From what I see, you want 3 Labels and 1 Image. A normal TableViewCell will not give you that ability to have 3 Labels. Unless you create it yourself. Below is a link for you to do so. I hope this is what you have in mind.
iPhone Custom Cell Tutorial