How to control my tableview cell label? - iphone

i am new in iPhone development. I am developing an app which contains tableview with label inside the cell. I have placed different label for each row like this if(indexpath.row == 0) like this for each cell row i have done it. But when i am scrolling the tableview my label are getting mixed. Please help me!
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.row == 2)
{
pendingListNumberLabel = [[UILabel alloc] init];
pendingListNumberLabel.font = [UIFont fontWithName:#"Helvetica" size:14];
pendingListNumberLabel.textAlignment = UITextAlignmentLeft ;
pendingListNumberLabel.textColor = [UIColor orangeColor];
pendingListNumberLabel.frame = CGRectMake(240, 6, 110, 30);
[pendingListNumberLabel setBackgroundColor:[UIColor clearColor]];
[cell addSubview:pendingListNumberLabel];
}
cell.textLabel.text = [affiliationsArray objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;

You are adding the UILabel as a subview, that is the mistake here.
Normally you would use a custom UITableViewCell for this kind of stuff, but a quick fix is this:
if (indexPath.row == 2)
{
pendingListNumberLabel = [[UILabel alloc] init];
pendingListNumberLabel.font = [UIFont fontWithName:#"Helvetica" size:14];
pendingListNumberLabel.textAlignment = UITextAlignmentLeft ;
pendingListNumberLabel.textColor = [UIColor orangeColor];
pendingListNumberLabel.frame = CGRectMake(240, 6, 110, 30);
[pendingListNumberLabel setBackgroundColor:[UIColor clearColor]];
pendingListNumberLabel.tag = 1337
[cell addSubview:pendingListNumberLabel];
} else {
for (UIView * subView in [cell subviews]) {
if ([subView tag] == 1337) {
[subView removeFromSuperview];
}
}
}

This is happening because you are using reusable cells and not in a right way ..
just replace your code -
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
with this -
UITableViewCell *cell = [[UITableViewCell alloc] init];
the above code can solve your issue, but this is not good if there are a large number of cells. In that case you should use reusable cells.

if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
else
{
UILabel *titleLbl = (UILabel *)[cell viewWithTag:1];
[titleLbl removeFromSuperview];
}
if (indexPath.row == 2)
{
pendingListNumberLabel = [[UILabel alloc] init];
pendingListNumberLabel.font = [UIFont fontWithName:#"Helvetica" size:14];
pendingListNumberLabel.textAlignment = UITextAlignmentLeft ;
pendingListNumberLabel.textColor = [UIColor orangeColor];
pendingListNumberLabel.tag = 1;// Write here.....................................
pendingListNumberLabel.frame = CGRectMake(240, 6, 110, 30);
[pendingListNumberLabel setBackgroundColor:[UIColor clearColor]];
[cell addSubview:pendingListNumberLabel];
}
cell.textLabel.text = [affiliationsArray objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
I think it will be helpful to you.

You can fix the problem of label mixing just by omitting the if (cell == nil) checking. Use the bellow code:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//if (cell == nil) // Need not this checking
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
Use the cell allocation directly as above :)

In your tableView:cellForRowAtIndexPath: method, first get the selected row like this
NSUInteger row = [indexPath row];
then use "row" to grab your data from an NSArray that corresponds to the tableView rows and assign it to your cell's textLabel's text property like this
NSString *string = [NSString stringWithFormat:#"%#",self.allItems objectAtIndex:row];
cell.textLabel.text = string;
return cell;
And of course you would have already created a UITableViewCell with a Reuse identifier like this :
static NSString *TableIdentifier = #"TableIdentifier";
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:TableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:TableIdentifier];
}
The key is using an array that corresponds exactly to your table rows. (ie - item 0 in the Array corresponds to row 0 in the tableView) then you can't go wrong.

For solve this issue you can put cell=nil; before checking cell== nil condition or create cell every time. It will work but it is not good Habit.
This also work for Text
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.row == 2)
{
pendingListNumberLabel = [[UILabel alloc] init];
pendingListNumberLabel.font = [UIFont fontWithName:#"Helvetica" size:14];
pendingListNumberLabel.textAlignment = UITextAlignmentLeft ;
pendingListNumberLabel.textColor = [UIColor orangeColor];
pendingListNumberLabel.frame = CGRectMake(240, 6, 110, 30);
[pendingListNumberLabel setBackgroundColor:[UIColor clearColor]];
[cell addSubview:pendingListNumberLabel];
}
cell.textLabel.text = [affiliationsArray objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;

Related

UITextView text in Tableview get Overrides?

In my app i am customised the UITableView Cell with four UITextViews. Whenever i added data to the tableview and reload it. The text in the UITableViewCell get override with the previous texts.
I tried different approaches but couldn't figure out what was the problem.
I am using TableView in View Controller.
Here is the Code i used in my table View cell?
if ( [tableView isEqual:self.tableActions])
{
//Setting the text empty
static NSString *CellIdentifier = #"ActionsIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
NSLog(#"Index Path %i",[indexPath row]);
ActionDetails *actiondetails = [actionArray objectAtIndex:[indexPath row]];
NSLog(#"Action Text %#",actiondetails.actions);
//Actions
actionText=[[UITextView alloc] initWithFrame:CGRectMake(10, 5, 230,30)];
actionText.font = [UIFont systemFontOfSize:17.0];
actionText.editable = NO;
actionText.textColor=[UIColor blackColor];
actionText.text = actiondetails.actions ;
actionText.userInteractionEnabled = NO;
actionText.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:actionText];
//Owner
ownerBy=[[UITextView alloc] initWithFrame:CGRectMake(230, 5, 230,30)];
ownerBy.font = [UIFont systemFontOfSize:17.0];
ownerBy.textColor=[UIColor blackColor];
ownerBy.textAlignment = UITextAlignmentCenter;
ownerBy.text = actiondetails.owner;
ownerBy.editable = NO;
ownerBy.userInteractionEnabled = NO;
ownerBy.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:ownerBy];
}
ScreenShot
Thanks for your help guys.
Much Appreciated.
This happened to me a other day, the solution that I came up with was removing all subview from the cell after creating the cell in the if statement.
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
if ([cell.contentView subviews]){
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
}
Remove cell identifier of Tableview otherwise take Customcell for the Tableview..
Just simple try this way :
{
//Setting the text empty
static NSString *CellIdentifier = #"ActionsIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
**// if(cell == nil) // comment this line in your code ,its work
// {**
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
**// }**
NSLog(#"Index Path %i",[indexPath row]);
ActionDetails *actiondetails = [actionArray objectAtIndex:[indexPath row]];
NSLog(#"Action Text %#",actiondetails.actions);
//Actions
actionText=[[UITextView alloc] initWithFrame:CGRectMake(10, 5, 230,30)];
actionText.font = [UIFont systemFontOfSize:17.0];
actionText.editable = NO;
actionText.textColor=[UIColor blackColor];
actionText.text = actiondetails.actions ;
actionText.userInteractionEnabled = NO;
actionText.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:actionText];
//Owner
ownerBy=[[UITextView alloc] initWithFrame:CGRectMake(230, 5, 230,30)];
ownerBy.font = [UIFont systemFontOfSize:17.0];
ownerBy.textColor=[UIColor blackColor];
ownerBy.textAlignment = UITextAlignmentCenter;
ownerBy.text = actiondetails.owner;
ownerBy.editable = NO;
ownerBy.userInteractionEnabled = NO;
ownerBy.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:ownerBy];
}
You are reusing the cell. Reused cell already has UITextview added to it.So you are overriding on it.
You must shift all your code of creating and adding UITextView to.
If(cell==nil)
{
}
After which you only need to set the Text to UITextview.
when your view did appear write reload your table view.
-(void)viewDidAppear:(BOOL)animated {
[tableView reloadData];
}
change your cellForRowAtIndexPath method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.section,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// your code
}
// your code
return cell;
}
for (id vw in [[cell contentView] subviews]) {
if ([vw isKindOfClass:[UILabel class]])
{
UILabel *label = (UILabel *)vw;
[label removeFromSuperview];
}
}
for (id vw in [cell subviews]) {
if ([vw isKindOfClass:[UILabel class]])
{
UILabel *label = (UILabel *)vw;
[label removeFromSuperview];
}
}

Enhance UITableView Scrolling

in my app i have an UITableView with 120 rows and every row has 1 UItextfeilds and 1 Buttons as show in the code bellow :
-(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];
}
else
{
}
UITextField *NameTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 10, 230, 28)];
NameTextField.borderStyle = UITextBorderStyleRoundedRect;
NameTextField.delegate = self;
NameTextField.textColor = UIColorFromRGB(0x2A1807);
NameTextField.font = [UIFont fontWithName:#"Helvetica" size:(17.0)];
NameTextField.font = [UIFont boldSystemFontOfSize:20];
NameTextField.tag = [indexPath row ];
NSString *temp = [self.sectionNames objectAtIndex:[indexPath section]];
NameTextField.text = [[self.myDataArray objectForKey:temp] objectAtIndex:[indexPath row]];
[cell.contentView addSubview:NameTextField];
[NameTextField release];
UIButton * aBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *wijzigImage = [UIImage imageNamed:#"btn_delete.png"];
aBtn.frame = CGRectMake(240, 10, 28, 26);
[aBtn setImage:wijzigImage forState:UIControlStateNormal];
[aBtn addTarget:self action:#selector(deleteCustomCellWithUIButton:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:aBtn];
return cell;
}
i have noticed that the Scrolling is slow and It doesn't go fluently.
any idea ?
Thanks
That's because you create textfields and buttons every time, add it inside if (cell == nil) {...}. The only thing that should be left outside that if is textField text setting.
Btw, your textfield leaks.
I found the solution :
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
else
{
UITextField *oldTextField = (UITextField *)[cell.contentView viewWithTag:999];
[oldTextField removeFromSuperview];
UIButton *oldBtn = (UIButton *)[cell.contentView viewWithTag:888];
[oldBtn removeFromSuperview];
}
NameTextField.tag = 999;
aBtn.tag = 88;

Data lose On UITableViewCell When UITableView Scrolling?

I am trying to implement UITableview based application. In my tableView their is 10 Section and each section having one row.
I want implement each section have Different type of ContentView(1-8 same ContentView 9th section Different ContentView). I did this code For that.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 10;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"Cell1";
static NSString *CellIdentifier2 = #"Cell2";
UITextField *textField;
UITextView *textView;
NSUInteger section=[indexPath section];
if(section == 9){
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if(cell==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1]autorelease];
textView=[[UITextView alloc]initWithFrame:CGRectMake(5, 5, 290, 110)];
[textView setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor
]];
[textView setTag:([indexPath section]+100)];
[cell.contentView addSubview:textView];
}else{
textView=(UITextView*)[cell.contentView viewWithTag:([indexPath section]+100)];
}
return cell;
}else {
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if(cell==nil){
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2]autorelease];
textField=[[UITextField alloc]initWithFrame:CGRectMake(5, 5, 290, 50)];
[textField setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]];
[textField setTag:([indexPath section]+100)];
[cell.contentView addSubview:textField];
}else{
textField=(UITextField*)[cell.contentView viewWithTag:([indexPath section]+100)];
}
return cell;
}
return nil;
}
My problem are:
1. After type some thing in the UITextField/UITextView i am scrolling in the UITableView. that time all data in the UITableViewCell(UITextField/UITextView) was lose, except last cell data.
2. If i create cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Instead of
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
Data will repeating . How can i over come this problem?
This line:
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
Should never appear in your data source cellForRowAtIndexPath method.
Apart from that, your code is OK, except that you are not setting the text field value anywhere. You need a model (such as an array of strings for the 10 textfield values). This model should be updated when the textfields are edited, and in your method above you copy the value back out of the model and into the textfield's text property:
textfield.text = [self.modelArray objectAtIndex:indexPath.section];
The table pools and reuses cells in an unpredictable fashion, so that subview of a cell that just scrolled off the bottom might reappear next at the top, or might be disposed of.
This is why you saw it partially work. The cell's subviews work okay until their cell gets reused or unloaded, then things move to the wrong place or data disappears.
The solution is that your table's datasource needs to hold onto it's own data. This is usually an array representing your model. Your case is a little unusual because you are using the text controls in your table as inputs, rather than display, which is more typical.
I suggest doing it like this:
// in #interface
#property (nonatomic, retain) NSMutableArray *sections;
// in #implementation
#synthesize sections=_sections;
// at some point before the view appears
self.sections = [NSMutableArray array];
for (int i=0; i<10; i++) {
UIControl *textControl;
if (i<9) {
textControl=[[UITextView alloc]initWithFrame:CGRectMake(5, 5, 290, 110)];
} else {
textControl=[[UITextField alloc]initWithFrame:CGRectMake(5, 5, 290, 50)];
}
[textControl setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]];
[textControl setTag:i+100];
[sections addObject:textControl];
[textControl release];
}
Now your cellForRowAtIndexPath is a little simpler:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier1 = #"Cell1";
static NSString *CellIdentifier2 = #"Cell2";
NSUInteger section=[indexPath section];
if(section == 9) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if(cell==nil) {
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1]autorelease];
}
} else {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if(cell==nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2]autorelease];
}
}
// who knows what subview this cell has? it might not have one, or it might have the wrong one
// just clean it up to be certain
for (UIView *view in cell.subviews) {
[view removeFromSuperView];
}
// get the textControl we set up for _this_ section/cell
UIControl *textControl = [self.sections objectAtIndex:section];
// now we have a fresh cell and the right textControl. drop it in
[cell addSubview:textControl];
return cell;
}
hey the reason is you are doing this things when cell is nil ? but you are not writing any code when cell is not nil.
look at this example , in this example i am adding image view in tableview cell , hence you can add textviews or any other views like this
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIImageView *imgView;
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100,0,100,62)];
[imgView setImage:[UIImage imageNamed:#"img.png"]];
imgView.tag = 55;
[cell.contentView addSubview:imgView];
[imgView release];
}
else
{
imgView = (id)[cell.contentView viewWithTag:55];
}
so as showin here imgView = (id)[cell.contentView viewWithTag:55]; you have to give tag to you and write code showing above in else..
Let you try to make the cell labels and textviews by using following code. It works for me.
if (tagvalue ==3) {
static NSString *CellIdentifier = #"Cell3";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
}
lbl7 = [[UILabel alloc] init];
[lbl7 setFont:[UIFont boldSystemFontOfSize:20]];
[cell.contentView addSubview:lbl7];
lbl7.backgroundColor = [UIColor clearColor];
lbl7.frame = CGRectMake(120, 5, 0, 40);
lbl7.tag = 70;
[lbl7 release];
lbl8 = [[UILabel alloc] init];
[lbl8 setFont:[UIFont boldSystemFontOfSize:18]];
[cell.contentView addSubview:lbl8];
lbl8.backgroundColor = [UIColor clearColor];
lbl8.textColor = [UIColor grayColor];
lbl8.frame = CGRectMake(120, 50, 0, 40);
lbl8.tag = 80;
[lbl8 release];
lbl7 = (UILabel*)[cell.contentView viewWithTag:70];
lbl8 = (UILabel*)[cell.contentView viewWithTag:80];
lbl7.text = [[rowsarray objectAtIndex:row]objectForKey:#"name"];
lbl8.text = [[rowsarray objectAtIndex:row]objectForKey:#"flavour"];
[lbl7 sizeToFit];
[lbl8 sizeToFit];
return cell;
}
if (tagvalue ==4) {
static NSString *CellIdentifier = #"Cell4";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
lbl9 = [[UILabel alloc] init];
[lbl9 setFont:[UIFont boldSystemFontOfSize:20]];
[cell.contentView addSubview:lbl9];
lbl9.backgroundColor = [UIColor clearColor];
lbl9.frame = CGRectMake(120, 5, 0, 40);
lbl9.tag = 90;
[lbl9 release];
lbl10 = [[UILabel alloc] init];
[lbl10 setFont:[UIFont boldSystemFontOfSize:18]];
[cell.contentView addSubview:lbl10];
lbl10.backgroundColor = [UIColor clearColor];
lbl10.textColor = [UIColor grayColor];
lbl10.frame = CGRectMake(120, 50, 0, 40);
lbl10.tag = 100;
[lbl10 release];
lbl9 = (UILabel*)[cell.contentView viewWithTag:90];
lbl10 = (UILabel*)[cell.contentView viewWithTag:100];
lbl9.text = [[rowsarray objectAtIndex:row]objectForKey:#"name"];
lbl10.text = [[rowsarray objectAtIndex:row]objectForKey:#"flavour"];
[lbl9 sizeToFit];
[lbl10 sizeToFit];
return cell;
}
I had same issue. Its not the problem with table class. The issue is at the place where you are calling this tableviewcontroller. First make the object of this call in .h and then allocate in .m, thats it..
When I was declaring in viewdidload like tbl *t = [self.storyboard...];, I was also facing the same problem. But when I put tbl *t; in .h problem solved.

overlapping subviews in UITableViewCell

I am stuck with this. I will explain with 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];
}
NSNumber *cellno=[NSNumber numberWithUnsignedInteger:indexPath.row];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 13, 15,18)];
imgView.image=[UIImage imageNamed:#"lock.png"];
tickView = [[UIImageView alloc] initWithFrame:CGRectMake(200, 13, 15,18)];
tickView.image=[UIImage imageNamed:#"tick.png"];
switch (indexPath.row) {
case 0:
cell.textLabel.text=#"apples";
if ([appDelegate.connected containsObject:cellno]) { //condition
[cell.contentView addSubview:tickView];
}else{
[cell.contentView addSubview:imgView];
}
break;
}
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
During the first time the tableview is loaded the 'imgView' subview is added to the cells content view and the after some operation the 'if' condition is satisfied and the 'tickView' is added.
The problem is, the oldview is not hidden or removed and hence both the images appear.
Help would be greatly appreciated
Instead of creating your imgView and tickView views in the cellForRowAtIndexPath method, create them when you create the cell and reuse them with the cell. Then you can do this:
...
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(240, 13, 15,18)];
imgView.image=[UIImage imageNamed:#"lock.png"];
tickView = [[UIImageView alloc] initWithFrame:CGRectMake(200, 13, 15,18)];
tickView.image=[UIImage imageNamed:#"tick.png"];
}
...
if ([appDelegate.connected containsObject:cellno]) { //condition
[imgView removeFromSuperview];
[cell.contentView addSubview:tickView];
}else{
[tickView removeFromSuperview];
[cell.contentView addSubview:imgView];
}

nsxmlparse Tableview Alignment

static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#""];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.backgroundView = [[[CustomCell alloc] init] autorelease];
cell.selectedBackgroundView = [[[CustomCell alloc] init] autorelease];
// At end of function, right before return cell:
cell.textLabel.backgroundColor = [UIColor clearColor];
// Configure the cell.
UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 45)];
UILabel *myLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(5, 55, 300, 20)];
UILabel *myLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 68, 300, 60)];
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
myLabel1.text=aBook.title;
myLabel2.text=aBook.pubDate;
myLabel3.text=aBook.description;
//myLabel1.lineBreakMode=UILineBreakModeCharacterWrap;
myLabel1.lineBreakMode=UILineBreakModeWordWrap;
myLabel1.numberOfLines=1;
myLabel1.textColor=[UIColor redColor];
myLabel1.backgroundColor = [UIColor blueColor];
myLabel1.font=[UIFont systemFontOfSize:14];
myLabel2.font=[UIFont systemFontOfSize:12];
myLabel3.textAlignment=UITextAlignmentLeft;
myLabel3.textColor=[UIColor blueColor];
myLabel3.lineBreakMode=UILineBreakModeCharacterWrap;
myLabel3.numberOfLines=3;
//myLabel3.lineBreakMode=UILineBreakModeWordWrap;
myLabel3.lineBreakMode=UILineBreakModeTailTruncation;
myLabel3.font=[UIFont systemFontOfSize:14];
//myLabel1.shadowColor=[UIColor redColor];
//myLabel1.backgroundColor=[UIColor grayColor];
[cell.contentView addSubview:myLabel1];
[cell.contentView addSubview:myLabel2];
[cell.contentView addSubview:myLabel3];
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[myLabel1 release];
[myLabel2 release];
[myLabel3 release];
//Set up the cell
return cell;
guys i have these codings. in mylabel 1 if i sets to 2, the text goes down so i cant see.
now finally i want to display 2lines of Title, 1 line of pubDate and 3 Lines of Description in one row.
i have displayed but i need some alignment i.e. above points, it should remove HTML(&mdash) tags
i dont know hw to customizing this.struggling with this.pls help me out
Subclass the UITableViewCell and use an that custom cell.
First problem here is that you add the labels each time your cell is displayed. But you may reuse some cells that all ready have in their contentView the labels so you can reuse these labels.
Second problem is the performance each time you alloc init and release 3 labels on each row is displayed. This will produce slow scroll speed on slow devices like iPhone 3G.
Take a look at apple's CustomTableViewCell sample code.
Here is an tutorial how to subClass UITableViewCell
With an custom cell class your method will Look like this
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[cell setLabel1String:aBook.title];
[cell setLabel2String:aBook.pubDate];
[cell setLabel3String:aBook.description];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}