When scrolling a tableview back again cells appear blank? - objective-c

After loading a tableview with custom cells all looks fine and scrolling down is fine.
When I scroll up the cells above appear blank. They are in fact fully functional as they can still be selected and then they go off to the details pages.
The code cellForRowAtIndexPath is executed for the returning row and returns the cell as I would expect with the right details. It just isn't displayed.
Any thoughts/help would be appreciated.
The code below is what I have been using.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = #"CustomCellIdentifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
for (id oneObject in nib) {
if ([oneObject isKindOfClass:[CustomCell class]]) {
cell = (CustomCell *)oneObject;
}
}
}
// Configure the cell.
Book *aBook = [appDelegate.sortedArray objectAtIndex:indexPath.row];
//name
NSString *trimmedString = [aBook.Name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
cell.nameLabel.text = trimmedString;
//catagory
NSString *trimmedExperience = [aBook.PrimaryExperience stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([trimmedExperience isEqualToString:#"1"]) {
cell.catagoryLabel.text = #"None";
}
else if([trimmedExperience isEqualToString:#"2"]){
cell.catagoryLabel.text = #"Limited";
}
else if ([trimmedExperience isEqualToString:#"4"]) {
cell.catagoryLabel.text = #"Full";
}
else {
cell.catagoryLabel.text = #"";
}
cell.distanceLabel.text = [NSString stringWithFormat:#"%1.1f",aBook.distance];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}

The problem lies with the xib file and how it loaded. To cut out the issue and gain total control the following code was substituted for the IB version of the custom cell.
static NSString *CellTableIdentifier = #"CellTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellTableIdentifier] autorelease];
CGRect nameLabelRect = CGRectMake(15, 5, 200, 15);
UILabel *nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect];
nameLabel.textAlignment = UITextAlignmentLeft;
nameLabel.font = [UIFont boldSystemFontOfSize:14];
nameLabel.tag = kNameTag;
[cell.contentView addSubview:nameLabel];
[nameLabel release];
CGRect catagoryLabelRect = CGRectMake(15, 26, 100, 15);
UILabel *catagoryLabel = [[UILabel alloc]initWithFrame:catagoryLabelRect];
catagoryLabel.textAlignment = UITextAlignmentLeft;
catagoryLabel.font = [UIFont systemFontOfSize:12];
catagoryLabel.tag = kExperienceTag;
[cell.contentView addSubview:catagoryLabel];
[catagoryLabel release];
CGRect distanceLabelRect = CGRectMake(210, 26, 70, 15);
UILabel *distanceLabel = [[UILabel alloc] initWithFrame:distanceLabelRect];
distanceLabel.textAlignment = UITextAlignmentRight;
distanceLabel.font = [UIFont boldSystemFontOfSize:12];
distanceLabel.tag = kDistanceTag;
[cell.contentView addSubview:distanceLabel];
[distanceLabel release];
}
Thanks for helping think this one through. Now the scrolling works perfectly.

Related

Duplicating subviews(UIButton and UIImageView) of UITableViewCell contentView on scrolling

In my tableView, in some cells i have added an imageView as subview of cell contentView. On scrolling tableView up and down these images duplicating on other cells also. But this problem doesn't occur always. Please suggest a solution.. I am using the following code.
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
if (friendsArray.count != 0)
{
NSString *str = [friendsIdArray objectAtIndex:indexPath.row];
if ([pendingRequests containsObject:str])
{
// Add image for pending item
UIImageView *pendImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pendng.png"]];
pendImage.frame = CGRectMake(220.0, 2.5, 70, 40);
pendImage.tag = indexPath.row;
[cell.contentView addSubview:pendImage];
}
}
}
NSString *object;
if (friendsArray.count == 0)
{
if ([cell.contentView viewWithTag:indexPath.row])
{
for (UIView *subview in [cell.contentView subviews])
{
[subview removeFromSuperview];
}
}
object = #"No friends added to the list";
cell.textLabel.textAlignment = UITextAlignmentCenter;
}
else
{
object = [friendsArray objectAtIndex:indexPath.row];
cell.textLabel.textAlignment = UITextAlignmentLeft;
if (![cell.contentView viewWithTag:indexPath.row])
{
NSString *str = [friendsIdArray objectAtIndex:indexPath.row];
if ([pendingRequests containsObject:str])
{
// Add image for pending item
UIImageView *pendImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pendng.png"]];
pendImage.frame = CGRectMake(220.0, 2.5, 70, 40);
pendImage.tag = indexPath.row;
[cell.contentView addSubview:pendImage];
}
}
}
The problem is solved now. I have used the following line inside the else case of creating new tableViewCell.
[cell.contentView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
I have edited my code as shown below :
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
else
{
[cell.contentView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
}
As Romit Mewada suggested you can use that but each time you have alloc & add new UIImageView.
Instead of that you can send nil to the image of that UIImageView.
Implement in this way.
if (cell == nil)
{
//get your cell or alloc & initialize
// create and add imageView
}
UIImageView* imageView = [cell.contentView viewWithTag:indexPath.row];
imageView.image = nil;
//do your other task, you can use the same imageView to add other image for other row.
Try this below code..
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
if (friendsArray.count != 0)
{
NSString *str = [friendsIdArray objectAtIndex:indexPath.row];
if ([pendingRequests containsObject:str])
{
// Add image for pending item
UIImageView *pendImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pendng.png"]];
pendImage.frame = CGRectMake(220.0, 2.5, 70, 40);
pendImage.tag = indexPath.row;
[cell.contentView addSubview:pendImage];
}
}
NSString *object;
if (friendsArray.count == 0)
{
if ([cell.contentView viewWithTag:indexPath.row])
{
for (UIView *subview in [cell.contentView subviews])
{
[subview removeFromSuperview];
}
}
object = #"No friends added to the list";
cell.textLabel.textAlignment = UITextAlignmentCenter;
}
else
{
object = [friendsArray objectAtIndex:indexPath.row];
cell.textLabel.textAlignment = UITextAlignmentLeft;
if (![cell.contentView viewWithTag:indexPath.row])
{
NSString *str = [friendsIdArray objectAtIndex:indexPath.row];
if ([pendingRequests containsObject:str])
{
// Add image for pending item
UIImageView *pendImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pendng.png"]];
pendImage.frame = CGRectMake(220.0, 2.5, 70, 40);
pendImage.tag = indexPath.row;
[cell.contentView addSubview:pendImage];
}
}
}
}

Adding label color close to each row in UITableView Objective-C

I have a UITableView with different sections and rows. I want to have different label or image like a icone close each cell but I don't know why it's looks like this picture:
Also scrolling changed the size of labels!
Here is the code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
if(indexPath.row == 0)
{
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
label.backgroundColor = [UIColor darkGrayColor];
[cell.contentView addSubview:label];
}
else if(indexPath.row == 1)
{
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
label.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:label]; }
else if(indexPath.row == 2)
{
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
label.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:label]; }
else{
}
[[cell textLabel] setText:contentForThisRow];
return cell;
}
In your code when user scroll table, labels created many times! You must check if color label already exist in cell and then set color
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
int colorLabelTag = 9999;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Because table cells are reusing we must check is label already in cell
UILabel* label = (UILabel *) [cell viewWithTag:colorLabelTag];
// If no - we create label
if (!label) {
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
label.tag = colorLabelTag;
[cell addSubview:label];
}
if(indexPath.row == 0)
{
label.backgroundColor = [UIColor darkGrayColor];
}
else if(indexPath.row == 1)
{
label.backgroundColor = [UIColor redColor];
}
else if(indexPath.row == 2)
{
label.backgroundColor = [UIColor redColor];
}
[[cell textLabel] setText:contentForThisRow];
return cell;
Unless you want more cusotomizing than this there is no need for subclassing the cell.
Let me give it a try:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
// If you need to add more custom views then create them here. E.g.:
UILabel *myLable = [[UILabel alloc] initWithFrame:CGRectMake(10,200,20,20)];
myLable.text = #"Some Value";
label.tag = 101; // use anything but 1. Better use a constant expression.
[cell.contentView addSubview:myLable];
}
switch (indexPath.row) {
case 1:
cell.imageView.image = [UIImage imageNamed:#"red.png"];
break;
case 2:
cell.imageView.image = [UIImage imageNamed:#"blue.png"];
break;
case 3:
cell.imageView.image = [UIImage imageNamed:#"green.png"];
break;
}
UILabel* theLabel = (UILabel *) [cell viewWithTag:101]; // again, a constant is of better style here.
if (theLabel { // it must exist. However, it is good practice to check upon that.
label.text = #"someOtherValue";
}
[[cell textLabel] setText:contentForThisRow];
// you may need to layout your subviews here.
return cell;
}

Incorrect text positioning in UITableViewCell in Xcode when creating cells programatically

I have created a UITableView with two cells, username and password using the code below. I am doing this programatically as static cells can't be used in a UIViewController (where this table must reside)
I have included a screenshot of the output the first time the view loads (the expected output) and the second time the view is shown (the incorrect output)
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:#"Cell"];
if( cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Login Ident"];
if (indexPath.row == 0) {
loginId = [[UITextField alloc] initWithFrame:CGRectMake(5, 0, 280, 21)];
loginId .placeholder = #"Email address";
loginId .autocorrectionType = UITextAutocorrectionTypeNo;
[loginId setClearButtonMode:UITextFieldViewModeWhileEditing];
cell.accessoryView = loginId;
}
if (indexPath.row == 1) {
password = [[UITextField alloc] initWithFrame:CGRectMake(5, 0, 280, 21)];
password.placeholder = #"Password";
password.secureTextEntry = YES;
password.autocorrectionType = UITextAutocorrectionTypeNo;
[password setClearButtonMode:UITextFieldViewModeWhileEditing];
cell.accessoryView = password;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
loginId.delegate = self;
password.delegate = self;
[loginId setText:[keychain objectForKey:(__bridge id)kSecAttrAccount]];
[password setText:[keychain objectForKey:(__bridge id)kSecValueData]];
[loginId addTarget:self
action:#selector(textFieldReturn:)
forControlEvents:UIControlEventEditingDidEndOnExit];
[password addTarget:self
action:#selector(textFieldReturn:)
forControlEvents:UIControlEventEditingDidEndOnExit];
[cell.contentView addSubview:loginId];
[cell.contentView addSubview:password];
//[_tableView addSubview:loginId];
//[_tableView addSubview:password];
}
return cell;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[_tableView reloadData];
}
My UITextField declarations
UITextField *loginId;
UITextField *password;
If anybody is able to help me out with this, I'd really appreciate it.
Looks like you are having issues with your cell reuse.
Here is how I would do it:
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Login Ident";
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:identifier];
if (nil == cell) {
// Within this if statement do any work that is not going to change over the lifetime of the cell.
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
// You was setting `selectionStyle` on every run so it should be
// safe to set here once
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
const NSInteger textFieldTag = 10; // <- No special meaning, just non-zero
UITextField *textField = (UITextField *)[cell viewWithTag:textFieldTag];
// I've followed a similar pattern to the cell reuse above for creating the textfield
// This ensures that we only create a textfield once per cell and then reuse it
if (nil == textField) {
textField = [[UITextField alloc] initWithFrame:CGRectMake(5, 0, 280, 21)];
textField .autocorrectionType = UITextAutocorrectionTypeNo;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.delegate = self;
textField.tag = textFieldTag;
[cell addSubview:textField];
}
// Next do any work to configure the different cells
if (indexPath.row == 0) {
textField.placeholder = #"Email address";
cell.accessoryView = loginId;
textField = textField;
loginId = textField;
} else if (indexPath.row == 1) {
textField.placeholder = #"Password";
textField.secureTextEntry = YES;
cell.accessoryView = password;
password = textField;
}
// ... rest of your work
return cell;
}
This is a issue because of the cell reusing. you need to change your cell a bit: add a textLabel in the if(cell==nil) branch, but configure it as username or password outside of the branch.
Firstly, the section:
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:#"Cell"];
if( cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Login Ident"];</code>
should be
static NSString *identifier = #"Login Ident";
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:identifier];
if( cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];</code>
Secondly, creating your text fields should be done outside the if (cell == nil) section.

Making UITableView without IB

I'm making UITableView without IB.
My UITableView has 2 styles UITableViewCell - first row style and other row style.
I use MTLable Class instead of UILabel.
Question:
This code's results is very strange.
First cell style and other cell style is mixed when next page scrolled.
I can't find my code's faults.
I need your advice. Thanks!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//firsT cELL
if(indexPath.row==0)
{
CGRect frame=CGRectMake(0,0,80, 60);
MTLabel *label1=[[MTLabel alloc] init];
label1.backgroundColor = [UIColor grayColor];
label1.frame=frame;
label1.text=#"123";
label1.tag = 1001;
[cell.contentView addSubview:label1];
label1.autoresizingMask = UIViewAutoresizingFlexibleTopMargin &UIViewAutoresizingFlexibleWidth & UIViewAutoresizingFlexibleLeftMargin & UIViewAutoresizingFlexibleRightMargin;
label1.contentMode = UIViewContentModeTopLeft;
[label1 setTextAlignment:MTLabelTextAlignmentRight];
[label1 release];
}
else
{
CGRect frame3=CGRectMake(0,0,80, 60);
MTLabel *label3=[[MTLabel alloc]init];
label3.frame=frame3;
label3.backgroundColor = [UIColor purpleColor];
label3.text=#"100";
label3.tag = 1003;
label3.autoresizingMask = UIViewAutoresizingFlexibleTopMargin &UIViewAutoresizingFlexibleWidth & UIViewAutoresizingFlexibleLeftMargin & UIViewAutoresizingFlexibleRightMargin;
label3.contentMode = UIViewContentModeTopLeft;
[label3 setTextAlignment:MTLabelTextAlignmentRight];
[cell.contentView addSubview:label3];
[label3 release];
}
}
FTS_book *book = [items objectAtIndex:indexPath.row];
if(indexPath.row==0)
{
MTLabel *label1 = (MTLabel*)[cell viewWithTag:1001];
label1.text= [NSString stringWithFormat:#"%d", book.chapter];
}
else
{
MTLabel *label3 = (MTLabel*)[cell viewWithTag:1003];
label3.text= [NSString stringWithFormat:#"%d", book.verse];
}
return cell;
}
Use different cell Id's for those two styles.

Can't Get Data Loaded on First Row of UITableView

I'm trying to parse an HTML Data using HTMLParser (by Ben Reeves) and display results on UITableView. For some reasons I'm able to show up results only on last row of the tableView. Here's the code snippet:
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSData *responseData = [request responseData];
NSError *error = [request error];
HTMLParser *parser = [[HTMLParser alloc] initWithData:responseData error:&error];
HTMLNode *bodyNode = [parser body];
arrayNodes = [bodyNode findChildrenWithAttribute:#"class" matchingName:#"foo" allowPartial:NO];
for (HTMLNode *arrayNode in arrayNodes) {
NSString *footitle = [arrayNode allContents];
NSLog(#"%#", footitle);
fooLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 200, 30)];
fooLabel.text = (#"%#", footitle);
fooLabel.textColor = [UIColor blackColor];
}
[self.fooTableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [arrayNodes count];
}
- (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];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
// Configure the cell.
// [self.arrayNodes objectAtIndex:indexPath.row];
[cell.contentView addSubview:fooLabel];
return cell;
}
Where am I making the mistake?
for (HTMLNode *arrayNode in arrayNodes) {
NSString *footitle = [arrayNode allContents];
NSLog(#"%#", footitle);
fooLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 200, 30)];
fooLabel.text = (#"%#", footitle);
fooLabel.textColor = [UIColor blackColor];
}
You are creating fooLabel's with same Frame size and location as many as arrayNodes array.
then in [cell.contentView addSubview:fooLabel]; it is showing you the last value that label is updated with.
take out that fooLabel from for loop.
in your cellForRowAtIndexPath:
HTMLNode* arrayNode = [arrayNodes objectAtIndex:[indexPath row]];
cell.textLabel.text = [NSString stringWithFormat:#"%#",[arrayNode allContents]];
You need to create the foolabel in your cellForRowAtindexPath method. Right now only one foolabel is created, and it is applied to each cell in turn and then set to the next cell, leaving it placed on the last cell. So instead you should do this:
-
(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];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
// Configure the cell.
HTMLNode *arrayNode = [arrayNodes objectAtIndex:indexPath.row];
NSString *footitle = [arrayNode allContents];
UILabel *fooLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 200, 30)];
fooLabel.text = (#"%#", footitle);
fooLabel.textColor = [UIColor blackColor];
[cell.contentView addSubview:fooLabel];
[fooLabel release];
return cell;
}