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;
}
Related
I am using Coredata and NSFetchedResultsController to store and retrieve Values and show them in a table view.I am creating a custom label in cellForRowAtIndexPath and displaying the value of an attribute 'lastname'. But I am getting wrong values.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *label = nil;
if(cell == nil){
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
label = [[[UILabel alloc]initWithFrame:CGRectMake(160,10,120,21)]autorelease];
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
//Configure the cell
List *list = [self.fetchedResultsController objectAtIndexPath:indexPath];
label.text = list.lastname;
}
[self configureCell:cell atIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
return cell;
}
the wierd part is that it is working fine if i remove the UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; line and the if condition.
You need to move
label.text = list.lastname;
outside of the
if(cell == nil)
The reason is, that the content inside of the if(cell == nil) will get called only x times, where x is the number of visible cells on the screen. As you scroll, the new cells are being reused and that's why they contain some incorrect value.
EDIT:
You will also need to move your
List *list = [self.fetchedResultsController objectAtIndexPath:indexPath];
outside of the if.
EDIT 2:
This is how it should look like:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *label = nil;
if(cell == nil){
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
label = [[[UILabel alloc]initWithFrame:CGRectMake(160,10,120,21)]autorelease];
label.backgroundColor = [UIColor clearColor];
label.tag=1;
[cell.contentView addSubview:label];
}
[self configureCell:cell atIndexPath:indexPath];
//Configure the cell
List *list = [self.fetchedResultsController objectAtIndexPath:indexPath];
label = (UILabel*)[cell.contentView viewWithTag:1];
label.text = list.lastname;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
return cell;
}
this should work for you
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;
}
In my cellForRow code I have the following and am getting an error saying nothing is being returned. What have I done wrong? Thanks.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
if([self.checkedIndexPath isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
This is an endless loop: UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
This should be:
static NSString *reuseIdentifier = #"some_identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] autorelease]
}
Your cellForRowAtIndexPath will look something like this (for the most Basic Table View)
- (UITableViewCell *) tableView :(UITableView *) tableView cellForRowAtIndexPath :(NSIndexPath *) indexPath
{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier ";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex :row];
cell.textLabel.font = [UIFont boldSystemFontOfSize:50];
return cell;
}
I m parsing json data and assigning it in my table view cell.where in the first row of tableview cell .i m assigning a static data(green text).if i scroll my tableview the data assigned in first table view cell is getting overlaped in my 6th and 7th tableview cell.below is the screen shotand the code.could u guys help me out
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.row == 0)
{
cell.textLabel.text=#"Pollenprognosen";
cell.textLabel.textColor=[UIColor greenColor];
cell.detailTextLabel.text=#"se dagens pollenprognos";
cell.detailTextLabel.font=[UIFont systemFontOfSize:([UIFont systemFontSize]-2)];
return cell;
}
else
{
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text=[story objectAtIndex:indexPath.row];
return cell;
}
}
I think it's because the cell is reused. Try this code instead:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.row == 0)
{
cell.textLabel.text=#"Pollenprognosen";
cell.textLabel.textColor=[UIColor greenColor];
cell.detailTextLabel.text=#"se dagens pollenprognos";
cell.detailTextLabel.font=[UIFont systemFontOfSize:([UIFont systemFontSize]-2)];
cell.accessoryType=UITableViewCellAccessoryNone;
return cell;
}
else
{
cell.textLabel.text=[story objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor blackColor];
cell.detailTextLabel.text=nil;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
}
Just make the else part as follows.
else
{
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text=[NSString stringWithFormat:#"Cell %d",indexPath.row];
cell.textLabel.textColor=[UIColor blackColor];
cell.detailTextLabel.text=#"";
return cell;
}
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.