UITableView if(cell == null) error - objective-c

I have this problem with my code, I am trying to add textfields to my cells. And it worked! But when I tried again afterwards it didn't. I debugged a little with NSLog and came to the conclusion that cell wasn't = nil and therefore my code wasn't being run.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Called1");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
cell.accessoryType = UITableViewCellAccessoryNone;
NSLog(#"Called?");
UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
playerTextField.adjustsFontSizeToFitWidth = YES;
playerTextField.textColor = [UIColor blackColor];
if([indexPath row] == 0) {
playerTextField.placeholder = #"yoyo1";
playerTextField.keyboardType = UIKeyboardTypeDefault;
playerTextField.returnKeyType = UIReturnKeyNext;
} else {
playerTextField.placeholder = #"yoyo2";
playerTextField.keyboardType = UIKeyboardTypeDefault;
playerTextField.returnKeyType = UIReturnKeyDone;
playerTextField.secureTextEntry = YES;
}
playerTextField.backgroundColor = [UIColor whiteColor];
playerTextField.autocorrectionType = UITextAutocorrectionTypeNo;
playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
playerTextField.textAlignment = UITextAlignmentLeft;
playerTextField.tag = 0;
playerTextField.clearButtonMode = UITextFieldViewModeNever;
[playerTextField setEnabled: YES];
[cell.contentView addSubview:playerTextField];
}
return cell;
}
The code NSLog(#"Called?") doesn't get called, but NSLog(#"Called1") does. Why?
Thanks.

Just a little change:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Called1");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
cell.accessoryType = UITableViewCellAccessoryNone;
NSLog(#"Called?");
//And then the rest of your cell configuration code...

Update your code to this, the way you initialize table cells is not correct.
[tableView dequeueReusableCellWithIdentifier:#"Cell"]; if this line returns non-nil your code will not correctly initialize the table cell. The logic is to get a new table cell by either dequeuing or re-using after you allocated a suitable table cell decorate it the same way.
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
return cell;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Called1");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [self reuseTableViewCellWithIdentifier:#"Cell" withIndexPath:indexPath];
}
cell.accessoryType = UITableViewCellAccessoryNone;
NSLog(#"Called?");
UITextField *playerTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
playerTextField.adjustsFontSizeToFitWidth = YES;
playerTextField.textColor = [UIColor blackColor];
if([indexPath row] == 0) {
playerTextField.placeholder = #"yoyo1";
playerTextField.keyboardType = UIKeyboardTypeDefault;
playerTextField.returnKeyType = UIReturnKeyNext;
} else {
playerTextField.placeholder = #"yoyo2";
playerTextField.keyboardType = UIKeyboardTypeDefault;
playerTextField.returnKeyType = UIReturnKeyDone;
playerTextField.secureTextEntry = YES;
}
playerTextField.backgroundColor = [UIColor whiteColor];
playerTextField.autocorrectionType = UITextAutocorrectionTypeNo;
playerTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
playerTextField.textAlignment = UITextAlignmentLeft;
playerTextField.tag = 0;
playerTextField.clearButtonMode = UITextFieldViewModeNever;
[playerTextField setEnabled: YES];
[cell.contentView addSubview:playerTextField];
return cell;
}

Related

Why we are checking if (cell == nil) in UITableViewController?

I am trying to implement UITableView based Application.For that I select UITableViewStyle is Group.In my TableView their is 15 section each section having 1 row.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 15;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==12)
{
return 120;
}
else
{
return 60;
}
}
I want add a UITextView on Section 12
For that i did following 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];
}
if ([indexPath section] == 12)
{
if([indexPath row]==0)
{
descriptionTextField=[[UITextView alloc] initWithFrame:CGRectMake(5, 8, 290, 106)];
descriptionTextField.font = [UIFont systemFontOfSize:15.0];
descriptionTextField.backgroundColor=[UIColor scrollViewTexturedBackgroundColor];
[descriptionTextField setDelegate:self];
[descriptionTextField setTag:2];
[descriptionTextField setText:#"Enter Location Description."];
descriptionTextField.keyboardType=UIKeyboardTypeDefault;
descriptionTextField.returnKeyType=UIReturnKeyNext;
descriptionTextField.textColor=[UIColor blackColor];
descriptionTextField.editable=YES;
descriptionTextField.autocapitalizationType=UITextAutocapitalizationTypeWords;
descriptionTextField.autocorrectionType=UITextAutocorrectionTypeDefault;
descriptionTextField.textAlignment=UITextAlignmentLeft;
UIToolbar* keboardToolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 32)];
UIBarButtonItem *extra=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *Done=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(keyboardDoneButtonActin:)];
[Done setWidth:65.0f];
[keboardToolBar setItems:[[[NSArray alloc]initWithObjects:extra,Done, nil]autorelease] ];
[extra release];
[Done release];
[keboardToolBar setTintColor:[UIColor blackColor]];
[keboardToolBar setAlpha:.70];
[descriptionTextField setInputAccessoryView:keboardToolBar];
[descriptionTextField setTag:101];
[cell.contentView addSubview:descriptionTextField];
[descriptionTextField release];
}
}
return cell;
}
In the initil stage the table view like this
if i am scrolling tableview up and down , then the uitextView section changed and it will display multiple location.
I can't understand my fault, why this happend?
if am implementing the above code in side the if(cell==nil)
- (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];
if ([indexPath section] == 12)
{
if([indexPath row]==0)
{
**/* implemention all code here*/**
[cell.contentView addSubview:descriptionTextField];
[descriptionTextField release];
}
}
return cell;
}
UITextView not disply, i think it is not allocating.
so what is the difference between code implementing in
if (cell == nil)
{
inside
}
if (cell == nil)
{
}
out side
The test if (cell == nil) handles the case where there are no reusable cells to dequeue, in which case you must create a new cell. When you create the new cell, you are responsible for constructing its view hierarchy.
NSString *CellIdentifier = [NSString stringWithFormat:#"%i",indexPath.row];
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
This can be used instead of
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
this is the simple example to write inside the cell ==nill
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
cell= nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[[cell.contentView viewWithTag:100+indexPath.row] removeFromSuperview];
UIView *selectview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 30)];
[selectview setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"strip_s12A-1_h.png"]]];
cell.selectedBackgroundView = selectview;
[selectview release];
UILabel *cellTitle=[[UILabel alloc]initWithFrame:CGRectMake(40, 0, 300, 30)];
//cellTitle.adjustsFontSizeToFitWidth=YES;
[cellTitle setBackgroundColor:[UIColor clearColor]];
[cellTitle setFont:[UIFont fontWithName:#"Arial Rounded MT Bold" size:17]];
[cellTitle setTextColor:[UIColor blackColor]];
cellTitle.tag = 100+indexPath.row;
cellTitle.text= [[[cellArray objectAtIndex:indexPath.section]objectAtIndex:indexPath.row] valueForKey:#"Des2"];
[cell.contentView addSubview:cellTitle];
[cellTitle release];
}
return cell;
}
i think will be enough right

UITableView Custom View still here even after reload

I made a very simply table view, with load more function.
I have added a custom view on the last cell with text "Load More"
After the user clicked the load more function, the rows increased successfully.
But the text "Load More" didn't disappear.
Please help.
Here is my code.
- (void)viewDidLoad {
[super viewDidLoad];
noRow = 10;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return noRow+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.row != noRow ) { // As long as we haven’t reached the +1 yet in the count, we populate the cell like normal
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
NSString *cellValue = [[NSNumber numberWithInt:[indexPath row]] stringValue];
cell.text = cellValue;
} // Ok, all done for filling the normal cells, next we probaply reach the +1 index, which doesn’t contain anything yet
else if(indexPath.row == noRow ) { // Here we check if we reached the end of the index, so the +1 row
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UILabel *loadMore;
loadMore =[[UILabel alloc]initWithFrame: CGRectMake(0,0,320,50)];
loadMore.textColor = [UIColor blackColor];
loadMore.highlightedTextColor = [UIColor darkGrayColor];
loadMore.backgroundColor = [UIColor clearColor];
loadMore.font=[UIFont fontWithName:#"Verdana" size:20];
loadMore.textAlignment=UITextAlignmentCenter;
loadMore.font=[UIFont boldSystemFontOfSize:20];
loadMore.text=#"Load More..";
[cell addSubview:loadMore];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == noRow){
NSLog(#"noRow Prev: %d", noRow);
noRow += 5;
NSLog(#"noRow After: %d", noRow);
[self.tableView reloadData];
}else{
NSLog(#"IndexPath.row: %d", indexPath.row);
}
}
You are reusing the and you are not removing the view that you have added for load more
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[[cell viewWithTag:121] removeFromSuperview];//remove this tag view
if (indexPath.row != noRow ) { // As long as we haven’t reached the +1 yet in the count, we populate the cell like normal
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
NSString *cellValue = [[NSNumber numberWithInt:[indexPath row]] stringValue];
cell.text = cellValue;
} // Ok, all done for filling the normal cells, next we probaply reach the +1 index, which doesn’t contain anything yet
else if(indexPath.row == noRow ) { // Here we check if we reached the end of the index, so the +1 row
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UILabel *loadMore;
loadMore =[[UILabel alloc]initWithFrame: CGRectMake(0,0,320,50)];
loadMore.textColor = [UIColor blackColor];
loadMore.highlightedTextColor = [UIColor darkGrayColor];
loadMore.backgroundColor = [UIColor clearColor];
loadMore.font=[UIFont fontWithName:#"Verdana" size:20];
loadMore.textAlignment=UITextAlignmentCenter;
loadMore.font=[UIFont boldSystemFontOfSize:20];
loadMore.text=#"Load More..";
loadMore.tag = 121;// just setting the tag
[cell addSubview:loadMore];
}
return cell;
}
you should add loadMore as a subview to cell.contentView not cell.
after that add this line in your cellForRow ..
for (UIView *view in [cell.contentView subviews])
{
[view removeFromSuperview];
}
Currently your load more is being reused and is present in subsequent cells if reused.

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.

Change label and icon positions of tableview's cell

it' possible to change position oh image and lable of my tableview's cell?
I would not use CustomCell..
I have tried this (for the label)..
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
else
return cell;
UIImage *image = nil;
cell.textLabel.text = #"text label";
UILabel *label = cell.textLabel;
CGRect frame = label.frame;
frame.origin.x = 62;
frame.origin.y = 62;
label.frame = frame;
image = [UIImage imageNamed:#"icon.png"];
cell.textLabel.font = [UIFont boldSystemFontOfSize:FONT_SIZE];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.imageView.image = image;
return cell;
}
Please take one UIview..and give frame size same as cell size...after that add label in that view...also whatever u want to add..add as a subview in that view...than add that view in cell .
All The Best
//table cell contains title,date,image
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell =nil;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
}
//tittle label
UILabel *titleLabel = [[UILabel alloc] initWithFrame:contentFrame] ;
titleLabel.numberOfLines = 2;
titleLabel.font = [UIFont boldSystemFontOfSize:12];
titleLabel.text = [currentTweet title];
[cell.contentView addSubview:titleLabel];
//date label
UILabel *dateLabel = [[UILabel alloc] initWithFrame:dateFrame];
dateLabel.font = [UIFont systemFontOfSize:10];
dateLabel.text = [currentTweet dateCreated];
[cell.contentView addSubview:dateLabel];
//image view
CGRect imageFrame = CGRectMake(2, 8, 40,50);
self.customImage = [[UIImageView alloc] initWithFrame:imageFrame];
customImage.image =image;
[cell.contentView addSubview:self.customImage];
return cell;

Changing specific cells in uitableview

I have a uitableview but I want to disable some cells and leave others enabled.
I used the following code to disable all cells except for the first in the cellForRowAtIndexPath:
if ([indexPath row] > 0 ) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.userInteractionEnabled = NO;
cell.textLabel.textColor = [UIColor lightGrayColor];
}
But instead of disabling all cells and enabling the first, it disabled all cells and enables the last. Why does this happen? And what can I do to get it to work?
BTW Here's my code for all cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
sectionContents = [[self listOfItems] objectAtIndex:[indexPath section]];
contentsForThisRow = [sectionContents objectAtIndex:[indexPath row]];
if ([indexPath row] > 0 ) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.userInteractionEnabled = NO;
cell.textLabel.textColor = [UIColor lightGrayColor];
}
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = contentsForThisRow;
return cell;
}
Replace your code of cellForRowAtIndexPath: with this one
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
sectionContents = [[self listOfItems] objectAtIndex:[indexPath section]];
contentsForThisRow = [sectionContents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = contentsForThisRow;
if ([indexPath row] > 0 ) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.userInteractionEnabled = NO;
cell.textLabel.textColor = [UIColor lightGrayColor];
}
return cell;
}
You don't show where you declare cell, but what you are doing is setting the userinteractionEnabled before getting the actual cell!
You have to put this customization code at the end of the method.
Also, try to declare the cell variable in the method, it has not to be used elsewhere.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
sectionContents = [[self listOfItems] objectAtIndex:[indexPath section]];
contentsForThisRow = [sectionContents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
if ([indexPath row] > 0 ) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.userInteractionEnabled = NO;
cell.textLabel.textColor = [UIColor lightGrayColor];
}
cell.textLabel.text = contentsForThisRow;
return cell;
}