I am having real troubles adding a UIImageView to a single UITableView cell. In my Storyboard I have a UITableViewController and four prototype dynamic cells. Three of them are just fine as normal left detail cells and they work fine. However one of them I need to have a UIImageView in.
So I have added said view to the cell, given the cell a custom class with a property so I can access the image view.
I have the following cells:
Title cell.
Image Cell.
URL Cell.
Notes Cell.
The contents of the table view change depending on whether the user wants to add a URL, note or image. So the user always sees the title cell with one of the others.
Here is my code and for whatever reason I just can't get the UIImageView to display on that image cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
//0 = Image
if ([[NSUserDefaults standardUserDefaults] integerForKey:#"NewScrapbookEntry"] == 0)
{
if (indexPath.row == 0) CellIdentifier = #"titleCell";
if (indexPath.row == 1) CellIdentifier = #"imageCell";
if (indexPath.row == 2) CellIdentifier = #"notesCell";
}
//1 = URL
else if ([[NSUserDefaults standardUserDefaults] integerForKey:#"NewScrapbookEntry"] == 1)
{
if (indexPath.row == 0) CellIdentifier = #"titleCell";
if (indexPath.row == 1) CellIdentifier = #"urlCell";
if (indexPath.row == 2) CellIdentifier = #"notesCell";
}
//2 = Notes
else if ([[NSUserDefaults standardUserDefaults] integerForKey:#"NewScrapbookEntry"] == 2)
{
if (indexPath.row == 0) CellIdentifier = #"titleCell";
if (indexPath.row == 1) CellIdentifier = #"notesCell";
}
ScrapbookImageDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[ScrapbookImageDetailCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
[cell setBackgroundColor:[UIColor colorWithRed:250.0f/255.0f green:250.0f/255.0f blue:250.0f/255.0f alpha:1.0f]];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.text = [firstSection objectAtIndex:indexPath.row];
cell.detailTextLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:13.0f];
cell.detailTextLabel.numberOfLines = 0;
cell.accessoryView = nil;
switch (indexPath.section)
{
case 0:
switch (indexPath.row)
{
case 0:
{
cell.detailTextLabel.text = scrapbook.title;
}
break;
case 1:
{
//0 = Image
if ([[NSUserDefaults standardUserDefaults] integerForKey:#"NewScrapbookEntry"] == 0)
{
cell.detailTextLabel.text = nil;
cell.iv.image = [UIImage imageNamed:#"image.png"];
}
//1 = URL
else if ([[NSUserDefaults standardUserDefaults] integerForKey:#"NewScrapbookEntry"] == 1)
{
cell.detailTextLabel.text = scrapbook.url;
}
//2 = Notes
else if ([[NSUserDefaults standardUserDefaults] integerForKey:#"NewScrapbookEntry"] == 2)
{
cell.detailTextLabel.text = scrapbook.notes;
}
}
break;
case 2:
{
cell.detailTextLabel.text = scrapbook.notes;
}
break;
default:
break;
}
break;
default:
break;
}
return cell;
}
Why are you changing the identifier value? The thing you just have to do is make custom cell, and in height for row, check if it's your desired row, and return with height of image else default size, and in cell for row method, again check desired row and add the image you want to add else make the imageview nil.
Related
i have a little social network and i tried to implement the ratings code into the table view, it judges ratings with stars... the problem is that the second section displays ratings from the first table section
picture:
so you can see it displays ratings in some of the tableview cells, just the last couple, why would it be doing this?
drawing code:
-(void)drawRect:(CGRect)rect
{
if(_shouldDrawRating)
{
self.desc_image.layer.cornerRadius = 8;
self.desc_image.clipsToBounds = YES;
_rating = [[UIStarRateView alloc]initWithFrame:CGRectMake(220,35,70,15)];
[_rating setMaxStars:5];
[_rating setFullImage:[UIImage imageNamed:#"StarFull.png"]];
[_rating setHalfImage:[UIImage imageNamed:#"StarHalf.png"]];
[_rating setEmptyImage:[UIImage imageNamed:#"StarEmpty.png"]];
[_rating setRate:_postRating];
[self addSubview:_rating];
}
}
cell rendering code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Regular_Cell *cell;
// Configure the cell...
if(indexPath.section == 0)
{
if(indexPath.row < NUMBER_OF_DYNAMIC_ROWS_RECENT)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"default"];
if(cell == nil)
{
cell = [[Regular_Cell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"default"];
}
NSDictionary *dict = self.recentArray[indexPath.row];
[cell setShouldDrawRating:YES];
[cell.title_label setText:dict[#"title"]];;
[cell.author_label setText:[NSString stringWithFormat:#"Author: %#",dict[#"author"]]];
[cell.views_label setText:[NSString stringWithFormat:#"Views: %#",dict[#"views"]]];
[cell setPoll_idFromString:[NSString stringWithFormat:#"%#",dict[#"id"]]];
//options for image view
cell.desc_image.contentMode = UIViewContentModeScaleAspectFill;
NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#",dict[#"imageURL"]]];
[cell.desc_image setImageURL:imageURL];
[cell setPostRating:indexPath.row+0.5];
}
else if(indexPath.row == NUMBER_OF_DYNAMIC_ROWS_RECENT)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"See More"];
if(cell == nil)
{
cell = [[Regular_Cell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"See More"];
}
}
else
{
cell = nil;
}
NSLog(#"%#",indexPath);
}
if(indexPath.section == 1)
{
if(indexPath.row < NUMBER_OF_DYNAMIC_ROWS_TOP)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"default"];
if(cell == nil)
{
cell = [[Regular_Cell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"default"];
}
NSDictionary *dict = self.topViewedArray[indexPath.row];
[cell.title_label setText:dict[#"title"]];
[cell.author_label setText:[NSString stringWithFormat:#"Author: %#",dict[#"author"]]];
[cell.views_label setText:[NSString stringWithFormat:#"Views: %#",dict[#"views"]]];
[cell setPoll_idFromString:[NSString stringWithFormat:#"%#",dict[#"id"]]];
//options for image view
cell.desc_image.contentMode = UIViewContentModeScaleAspectFill;
NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#",dict[#"imageURL"]]];
[cell.desc_image setImageURL:imageURL];
}
else if(indexPath.row == NUMBER_OF_DYNAMIC_ROWS_TOP)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"See More"];
if(cell == nil)
{
cell = [[Regular_Cell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"See More"];
}
}
else
{
cell = nil;
}
NSLog(#"%#",indexPath);
}
return cell;
}
In section 1 you have to set the boolean to NO,It doesnt have a proper value now in section 1.I think you miss this line.
[cell setShouldDrawRating:NO];
Not only in Section 1 where ever you create the cell it must be implemented,Otherwise can cause problem of inconsistency in drawing
Replace code for setting cell labels, images etc from tableView:cellForRowAtIndexPath: and paste it in tableView:willDisplayCell:forRowAtIndexPath:. And setShouldDrawRating: in each section, not only in section number 0.
Spent hours and hours and no use.I don't understand whether I was not effective in searching(googling) or is it that there are less questions on this or I might have committed some mistake while implementing the answers of experts!
I know there are several questions on setting accessory type check mark for one row and none for other rows in a section,traced out posts here and there.
I have 2 sections in my table view.By default I want the 1st row in each section to be selected i.e. with accessory view check mark.Now from here upon user selection of a row,I want the check mark to be visible on selected row only.I have tried declaring two index paths to keep track of row selection in each section.Here is my implementation code:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.row,indexPath.section];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (indexPath.section == 0)
{
if(self.firstSectionIndex == indexPath)
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
if (indexPath.section == 1)
{
if(self.secondSectionIndex == indexPath)
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
switch (indexPath.section)
{
case 0:
{
if (indexPath.row == 0)
{
if(self.firstSectionIndex != indexPath)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = #"Yes";
}
if (indexPath.row == 1)
{
cell.textLabel.text = #"No";
}
}
break;
case 1:
{
if (indexPath.row == 0)
{
if(self.secondSectionIndex != indexPath)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = #"Easy";
}
if (indexPath.row == 1)
{
cell.textLabel.text = #"Medium";
}
if (indexPath.row == 2)
{
cell.textLabel.text = #"Hard";
}
}
break;
default:
break;
}
}
tableView.backgroundColor = [UIColor clearColor];
tableView.backgroundView = nil;
return cell;
}
in my didSelectRowAtIndexPath,I have done the following:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
self.firstSectionIndex = indexPath;
}
if (indexPath.section == 1)
{
self.secondSectionIndex = indexPath;
}
[tableView reloadData];
}
I have searched for the state of cell selection to be saved for long term reference,in quest of it,I found some useful link here.
But it is selecting multiple cells and accessory type check mark is being applied for all rows in section.I don't understand what's wrong.Can any one please guide me on this!!
Thanks all in advance :)
you can achieve it using this bellow implementation of code :-
EDITED
.h file
NSMutableArray *firstSelectedCellsArray;
NSMutableArray *secondSelectedCellsArray;
NSMutableArray *ThirdSelectedCellsArray;
in .m file
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
NSNumber *rowNumber = [NSNumber numberWithUnsignedInt:indexPath.row];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
{
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
if ( [firstSelectedCellsArray containsObject:rowNumber] )
{
[firstSelectedCellsArray removeObject:rowNumber];
}
else
{
[firstSelectedCellsArray addObject:rowNumber];
}
}
else if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
{
if ( [firstSelectedCellsArray containsObject:rowNumber] )
{
[firstSelectedCellsArray removeObject:rowNumber];
}
else
{
[firstSelectedCellsArray addObject:rowNumber];
}
selectedCell.accessoryType = UITableViewCellAccessoryNone;
}
}
if (indexPath.section == 1)
{
NSNumber *rowNumber = [NSNumber numberWithUnsignedInt:indexPath.row];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
{
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
if ( [secondSelectedCellsArray containsObject:rowNumber] )
{
[secondSelectedCellsArray removeObject:rowNumber];
}
else
{
[secondSelectedCellsArray addObject:rowNumber];
}
}
else if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
{
if ( [secondSelectedCellsArray containsObject:rowNumber] )
{
[secondSelectedCellsArray removeObject:rowNumber];
}
else
{
[secondSelectedCellsArray addObject:rowNumber];
}
selectedCell.accessoryType = UITableViewCellAccessoryNone;
}
}
if (indexPath.section == 2)
{
NSNumber *rowNumber = [NSNumber numberWithUnsignedInt:indexPath.row];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
{
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
if ( [ThirdSelectedCellsArray containsObject:rowNumber] )
{
[ThirdSelectedCellsArray removeObject:rowNumber];
}
else
{
[ThirdSelectedCellsArray addObject:rowNumber];
}
}
else if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
{
if ( [ThirdSelectedCellsArray containsObject:rowNumber] )
{
[ThirdSelectedCellsArray removeObject:rowNumber];
}
else
{
[ThirdSelectedCellsArray addObject:rowNumber];
}
selectedCell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
NOW in cellForRowAtIndexPath put littel piece of code:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.section==0)
{
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [firstSelectedCellsArray containsObject:rowNsNum] )
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
if(indexPath.section==1)
{
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [secondSelectedCellsArray containsObject:rowNsNum] )
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
if(indexPath.section==2)
{
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [ThirdSelectedCellsArray containsObject:rowNsNum] )
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
return cell;
}
DEMO LINK
http://www.sendspace.com/file/z6oxg2
ScreenShot:
I have found a precise and perfect solution,thanks to Lithu T.V and Nithin Gobel for guiding me in the right direction.Unfortunately their solutions didn't help me in achieving 1 check mark per section,but multiple check marks in fact.So I thought of saving the selected row in user defaults and for initially selecting 1st row of each section,we declare first and second section index,assign to index path and then assign cell accessory view as check mark.Here we go,let's first deal with cellForRowAtIndexPath:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.row,indexPath.section];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
NSUserDefaults *savedState = [NSUserDefaults standardUserDefaults];
switch (indexPath.section)
{
case 0:
{
NSNumber *indexNumber = [NSNumber numberWithInteger:indexPath.row];
if([[savedState objectForKey:#"firstSectionIndex"] isEqual: indexNumber])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.firstSectionIndex = indexPath;
}
if (indexPath.row == 0)
{
NSObject * checkedCellObject = [savedState objectForKey:#"firstSectionIndex"];
if(checkedCellObject == nil)
{
self.firstSectionIndex = indexPath;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
cell.textLabel.text = #"Yes";
}
if (indexPath.row == 1)
{
cell.textLabel.text = #"No";
}
}
break;
case 1:
{
NSNumber *indexNumber = [NSNumber numberWithInteger:indexPath.row];
if([[savedState objectForKey:#"secondSectionIndex"] isEqual: indexNumber])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
secondSectionIndex = indexPath;
}
if (indexPath.row == 0)
{
NSObject * checkedCellObject = [savedState objectForKey:#"secondSectionIndex"];
if(checkedCellObject == nil)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
secondSectionIndex = indexPath;
}
cell.textLabel.text = #"Easy";
}
if (indexPath.row == 1)
{
cell.textLabel.text = #"Medium";
}
if (indexPath.row == 2)
{
cell.textLabel.text = #"Hard";
}
}
break;
default:
break;
}
}
tableView.backgroundColor = [UIColor clearColor];
tableView.backgroundView = nil;
return cell;
}
Please observe in each case,we are checking if the checked state is in saved user defaults,if it is nil,we check 1st cell(row) of each section,here we go with table view delegate method,did select row at index path:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.section == 0)
{
UITableViewCell *checkCell = [tableView cellForRowAtIndexPath:indexPath];
if(firstSectionIndex && firstSectionIndex != indexPath)
{
UITableViewCell *uncheckCell = [tableView cellForRowAtIndexPath:firstSectionIndex];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
self.firstSectionIndex = indexPath;
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:firstSectionIndex.row] forKey:#"firstSectionIndex"];
checkCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
if (indexPath.section == 1)
{
UITableViewCell *checkCell = [tableView cellForRowAtIndexPath:indexPath];
if(secondSectionIndex)
{
UITableViewCell *unchekCell = [tableView cellForRowAtIndexPath:secondSectionIndex];
unchekCell.accessoryType = UITableViewCellAccessoryNone;
}
self.secondSectionIndex = indexPath;
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:secondSectionIndex.row] forKey:#"secondSectionIndex"];
checkCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
That's it,we have successfully accomplished one check mark per section and also save the accessory state of cell in user defaults for long term reference.
Hope this helps some one,thanks :)
for implementing checkmark
Keep an index array for selected indexes
In the cellFor Row method check if current indexpath is there in the
selected index array.
if true
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
In didSelect method if checkmarked.Remove index from array as well
as set the accessory type none
If accessory none add the checkmark and add the index into array .
EDIT
In case what you need is the single selection everytime you select a cell remove all index from array and add that only and then reload tableview
I have the similar requirement where I need to select a single option per section and show the tick mark and I have achieved the same with simple steps. You do not need to reload the section or table at all.
Step 1:
Keep a hash-table/dictionary to your model which keeps the record of selected IndexPath per section type.
Example -
class DeviceSettingModel {
var indexPathSelectionDict: [DeviceSettingSectionType: IndexPath] = [:]
}
enum DeviceSettingSectionType: Int {
case audioDevices
case videoDevices
case videoQualities
}
Step 2:
Update the selection in tableView(didSelectAtRow) delegate:
Example -
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let settingModel = settingModel,
let type: DeviceSettingSectionType = DeviceSettingSectionType(rawValue: indexPath.section)
else { return }
// other data and view related updating...
// unselect first on the same section.
if let previousIndex = settingModel.indexPathSelectionDict[type] {
if let cell = tableView.cellForRow(at: previousIndex) {
cell.accessoryType = .none
}
}
// update selected indexPath into the model and put a tick mark
settingModel.indexPathSelectionDict[type] = indexPath
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark
}
}
That's it, you can see below effect.
Please take a look at the image.
I have a UITableView with custom cells and 2 sections. Once the UITableView populates enough cells that they all can't be shown on the screen, the last cell in the last section appears at the very top of the screen. The cell shouldn't even be visible in the UITableView since it's the last cell, and it is appearing completely outside the UITableView, at the top of the screen. I'm at a loss for even knowing where to begin to look in my code for this issue. I've only created a couple of UITableViews so I'm definitely not an expert on all the potential causes for this bug. The Table is set up through IB and the initialization code is in viewWillAppear, although there isn't much set up code. I am currently reading up on UITableView's scrollView, along with contentInset to see if there might be an explanation for this behavior. Has anyone encountered such a bug? I feel like it's unique enough that it may not be necessary to comb through all my code to determine. Regardless, I will post the relevant code below. Thank you for your help!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *uniqueIdentifierForPlayerCell = #"customPlayerCell";
//Initialize PlayerTableViewCell and set it's properties
PlayerTableViewCell *playerCell = nil;
playerCell = (PlayerTableViewCell *)[tableView dequeueReusableCellWithIdentifier:uniqueIdentifierForPlayerCell];
if (playerCell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"PlayerTableViewCell" owner:nil options:nil];
for(id currentObject in topLevelObjects) {
if([currentObject isKindOfClass:[PlayerTableViewCell class]]) {
playerCell = (PlayerTableViewCell *)currentObject;
break;
}
}
}
playerCell.textLabel.opaque = NO;
playerCell.textLabel.textColor = self.textColor;
playerCell.textLabel.font = [UIFont systemFontOfSize:18.0];
playerCell.textLabel.backgroundColor = [UIColor clearColor];
playerCell.selectionStyle = UITableViewCellSelectionStyleNone;
playerCell.accessoryButton.hidden = YES;
playerCell.reorderButton.hidden = YES;
NSString *uniqueIdentifierForAlleyCell = #"customAlleyCell";
//Initialize AlleyTableViewcell and set it's properties
AlleyTableViewCell *alleyCell = nil;
alleyCell = (AlleyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:uniqueIdentifierForAlleyCell];
if (alleyCell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"AlleyTableViewCell" owner:nil options:nil];
for(id currentObject in topLevelObjects) {
if([currentObject isKindOfClass:[AlleyTableViewCell class]]) {
alleyCell = (AlleyTableViewCell *)currentObject;
break;
}
}
}
alleyCell.textLabel.opaque = NO;
alleyCell.textLabel.textColor = self.textColor;
alleyCell.textLabel.font = [UIFont systemFontOfSize:18.0];
alleyCell.textLabel.backgroundColor = [UIColor clearColor];
alleyCell.selectionStyle = UITableViewCellSelectionStyleNone;
alleyCell.accessoryButton.hidden = YES;
alleyCell.deleteButton.hidden = YES;
//Alternate background colors for each section where section 0 is Alley section and section 1 is Player section. Stores the RGB values of the specific background color to the specific cell for future use
if(indexPath.section == 0 && (indexPath.row %2 == 0)) {
alleyCell.contentView.backgroundColor = self.backgroundColor;
alleyCell.isRed = 95.0/255.0;
alleyCell.isGreen = 94.0/255.0;
alleyCell.isBlue = 94.0/255.0;
self.alleyField.backgroundColor = self.backgroundColor;
}
else if(indexPath.section == 0 && (indexPath.row %2 == 1)){
alleyCell.contentView.backgroundColor = self.alternateColor;
alleyCell.isRed = 92.0/255.0;
alleyCell.isGreen = 92.0/255.0;
alleyCell.isBlue = 92.0/255.0;
self.alleyField.backgroundColor = self.alternateColor;
}
else if(indexPath.section == 1 && (indexPath.row %2 == 0)) {
playerCell.contentView.backgroundColor = self.backgroundColor;
playerCell.isRed = 95.0/255.0;
playerCell.isGreen = 94.0/255.0;
playerCell.isBlue = 94.0/255.0;
self.playerField.backgroundColor = self.backgroundColor;
}
else if(indexPath.section == 1 && (indexPath.row %2 == 1)) {
playerCell.contentView.backgroundColor = self.alternateColor;
playerCell.isRed = 92.0/255.0;
playerCell.isGreen = 92.0/255.0;
playerCell.isBlue = 92.0/255.0;
self.playerField.backgroundColor = self.alternateColor;
}
//If Alley array is empty, add alley TextField and return cell
if(indexPath.section == 0) {
if([self.allAlleys count] == 0) {
[alleyCell addSubview:self.alleyField];
self.alleyField.text = nil;
return alleyCell;
}
//Else set the alley name to the nameLabel text and return cell
else {
if(indexPath.row < [self.allAlleys count]) {
alleyCell.textLabel.text = [self.allAlleys objectAtIndex:indexPath.row];
if([alleyCell.textLabel.text isEqualToString: self.selectedAlley]) {
alleyCell.accessoryButton.hidden = NO;
}
else {
alleyCell.accessoryButton.hidden = YES;
}
return alleyCell;
}
//Add alley TextField to the last cell of the UITableView and return cell
else {
[alleyCell addSubview:self.alleyField];
self.alleyField.text = nil;
return alleyCell;
}
}
}
//If Player array is empty, add Player TextField and return cell
else if(indexPath.section == 1) {
if([self.bowlrNames count] == 0) {
[playerCell addSubview:self.playerField];
self.playerField.text = nil;
return playerCell;
}
//Else set the Player name to the nameLabel text and return cell
else {
if(indexPath.row < [self.bowlrNames count]) {
playerCell.textLabel.text = [self.bowlrNames objectAtIndex:indexPath.row];
for(int i = 0; i < [self.selectedBowlrs count]; i++) {
if([playerCell.textLabel.text isEqualToString: [self.selectedBowlrs objectAtIndex:i]]) {
playerCell.accessoryButton.hidden = NO;
playerCell.reorderButton.hidden = NO;
}
}
return playerCell;
}
//Add Player textField to the last cell of the UITableView and return cell
else {
[playerCell addSubview:self.playerField];
self.playerField.text = nil;
return playerCell;
}
}
}
}
//Actions for selecting a row in the UITable
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//If it is in the Alley section
if(indexPath.section == 0) {
//If the selected cell is the same as the last selected cell
if(self.lastIndexPathForAlleyCells && indexPath.row == self.lastIndexPathForAlleyCells.row) {
[tableView beginUpdates];
self.lastIndexPathForAlleyCells = nil;
AlleyTableViewCell *previousCell = (AlleyTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
//[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
previousCell.selected = NO;
self.selectedAlley = nil;
previousCell.accessoryButton.hidden = YES;
[tableView endUpdates];
return;
}
//Else the selected cell is not the last selected cell
AlleyTableViewCell *previousCell = (AlleyTableViewCell *)[tableView cellForRowAtIndexPath:self.lastIndexPathForAlleyCells];
previousCell.selected = NO;
previousCell.accessoryButton.hidden = YES;
AlleyTableViewCell *cell = (AlleyTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.selected = YES;
cell.accessoryButton.hidden = NO;
self.selectedAlley = cell.textLabel.text;
self.lastIndexPathForAlleyCells = indexPath;
[self.tableView reloadData];
}
else if(indexPath.section == 1) {
PlayerTableViewCell *cell = (PlayerTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
//If player is not already selected
if([self playerIsSelected:cell.textLabel.text] == NO) {
//Set selected to YES and reveal buttons
cell.selected = YES;
cell.accessoryButton.hidden = NO;
cell.reorderButton.hidden = NO;
//If 8 Players are currently selected, UIAlertView informs user they have reached the limit
if([self.selectedBowlrs count] == 8) {
NSString *message = #"You can only select up to 8 players";
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Max Players Selected" message:message delegate:self cancelButtonTitle:[self okButtonTitle] otherButtonTitles:nil, nil];
alertView.tag = TAG_FULLGAME;
[alertView show];
}
else {
//Insert name into selectedBowlrs array
[self.selectedBowlrs insertObject:cell.textLabel.text atIndex:0];
//Reorder bowlrNames array
NSString *bowlrName = [self.bowlrNames objectAtIndex:indexPath.row];
[self.bowlrNames removeObjectAtIndex:indexPath.row];
[self.bowlrNames insertObject:bowlrName atIndex:0];
//Move selected row to top of section
[self moveIndexPathToTop:indexPath];
}
}
//Else the player is already selected
else {
//Set selected to NO and hide buttons
cell.selected = NO;
cell.accessoryButton.hidden = YES;
cell.reorderButton.hidden = YES;
[self.selectedBowlrs removeObject:cell.textLabel.text];
[self.bowlrNames removeObject:cell.textLabel.text];
[self.bowlrNames insertObject:cell.textLabel.text atIndex:[self.selectedBowlrs count]];
[self moveIndexPathToMiddle:indexPath];
for(int i = 0; i < [self.selectedBowlrs count]; i++) {
NSLog(#"%#", [self.selectedBowlrs objectAtIndex:i]);
}
}
}
[self performSelector:#selector(reloadData) withObject:nil afterDelay:.25];
}
//Returns the number of rows for each section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
//For Alley section
case 0:
//If the array is empty, return 1 row for the Alley Textfield
if([self.allAlleys count] == 0)
return 1;
//Else return the number of elements in the array + 1 for the Alley Textfield
else
return [self.allAlleys count] + 1;
break;
//For Player section
case 1:{
//If the array is empty, return 1 row for the Player Textfield
if([self.bowlrNames count] == 0)
return 1;
//Else return the number of elements in the array + 1 for the Player Textfield
else
return [self.bowlrNames count] + 1;
break;
}
default:
break;
}
return 0;
}
//Set up appearance for section headers
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.tableView.bounds.size.width, 25.0)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
CGFloat isRed = 84.0/255.0;
CGFloat isGreen = 84.0/255.0;
CGFloat isBlue = 84.0/255.0;
self.headerColor = [[UIColor alloc]initWithRed:isRed green:isGreen blue:isBlue alpha:0.5];
headerLabel.backgroundColor = self.headerColor;
headerLabel.opaque = NO;
headerLabel.textColor = self.textColor;
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont systemFontOfSize:18.0];
headerLabel.frame = CGRectMake(0.0, 0.0, self.tableView.bounds.size.width, 25.0);
if (section == 0) {
[headerView setBackgroundColor:self.backgroundColor];
headerLabel.text = #"Alley";
[headerView addSubview:headerLabel];
}
else {
[headerView setBackgroundColor:self.backgroundColor];
headerLabel.text = #"Bowlr";
[headerView addSubview:headerLabel];
}
return headerView;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
//Used to delete row of UITable
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
self.indexPathForEditing = indexPath;
if(editingStyle == UITableViewCellEditingStyleDelete) {
//For Alley section
if(indexPath.section == 0) {
//Set up UIAlertView
NSString *message = #"Are you sure?";
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Delete Alley?" message:message delegate:self cancelButtonTitle:[self cancelButtonTitle] otherButtonTitles:[self deleteButtonTitle], nil];
alertView.tag = TAG_Alley;
[alertView show];
}
//For Player section
else if(indexPath.section == 1) {
//Set up UIAlertView
NSString *message = #"All information for this Bowlr will be deleted";
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Delete Bowlr?" message:message delegate:self cancelButtonTitle:[self cancelButtonTitle] otherButtonTitles:[self deleteButtonTitle], nil];
alertView.tag = TAG_Player;
[alertView show];
}
}
[self performSelector:#selector(reloadData) withObject:nil afterDelay:.25];
}
Okay, I think there's two basic things you need to remember here.
(1) You must never add something to the cell as a subview. Add it only to the cell's contentView. This code is wrong: [alleyCell addSubview:self.alleyField];
(2) These cells are being reused. You may have 100 rows but only 10 cells, being constantly reused as the user scrolls. Therefore you must completely set up every cell from an empty state, and if it isn't in the empty state to start with, you need to get it into the empty state. If you add a textfield to a cell in one pass thru cellForRowAtIndexPath, you must remember to remove the textfield in every other pass thru cellForRowAtIndexPath, or the textfield can show up in other rows because another row might reuse that same cell.
(See also the discussion towards the end of this section of my book: http://www.apeth.com/iOSBook/ch21.html#_the_three_big_questions It discusses a simple consequence of forgetting that the cells are reused.)
Also, I need hardly add that frame matters. You are adding a subview, but I have no idea what its frame is. With the right (meaning wrong) frame value, that subview could appear outside the cell. Also, are you using cells of different heights? If so, remember that because the cell is being reused, its height can change. Thus, autoresizing behavior can cause the frame to change.
following code sets cell texts and add a checkMark only to last selected. Always only one cell check marked and works properly excepting when it is displayed for first time. So, text is not showed for that cell (only that one) until you press any other cell. For example, if cellPos = 4 when viewDidLoad, that cell will not contain text.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* cellIdentifier = #"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
{
cell = (UITableViewCell*) [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryNone;
if (indexPath.section == 0) {
if(indexPath.row == cellPos)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.selected = YES;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selected = NO;
}
switch (indexPath.row) {
case 0:
cell.textLabel.text = #"English";
cell.imageView.image = [UIImage imageNamed:#"english.png"];
break;
case 1:
cell.textLabel.text = #"Español";
cell.imageView.image = [UIImage imageNamed:#"spanish.png"];
break;
case 2:
cell.textLabel.text = #"Deutsch";
cell.imageView.image = [UIImage imageNamed:#"germany.png"];
break;
case 3:
cell.textLabel.text = #"Français";
cell.imageView.image = [UIImage imageNamed:#"french.png"];
break;
case 4:
cell.textLabel.text = #"Italiano";
cell.imageView.image = [UIImage imageNamed:#"italian.png"];
break;
default:
break;}
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
cellPos = indexPath.row;
[tableView reloadData];
}
Have you tried moving the if-else block after the switch block? This way you set the text of the cell before you set the cell to selected. The fact that it only occurs the first time suggests to me that it may be an order of operations problem.
i am working on an iphone project that shows an artwork at the top tableview section (section 0) and shows a list of items in (section 1). the tableview looks like that:
section0:
ARTWORK IMAGE
section1:
cell1: text1
cell2: text2
.
.
. and so goe like that.
but strangely the program shows another imageview at the bottom of the page when i scroll down, but it shouldnt show that image because its not section 0.
heres the code for my program :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
switch (section) {
case 0:
return 1;
break;
case 1:
return [diskArray count];
break;
default:
return 0;
break;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section) {
case 0:
return 225;
break;
default:
return 44;
break;
}
}
// Customize the appearance of table view cells.
- (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];
}
// Configure the cell...
if(indexPath.section == 0)
{
if(indexPath.row == 0)
{
NSLog(#"adding image to the cell");
UIImage *img = [UIImage imageNamed:#"artwork.jpeg"];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(47, 0, 225, 225)];
imgView.image = img;
[cell addSubview:imgView];
[imgView release];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.text = nil;
}
}
else if(indexPath.section == 1){
cell.textLabel.text = [diskArray objectAtIndex:indexPath.row];
cell.backgroundColor = [UIColor darkGrayColor];
cell.textLabel.textColor = [UIColor whiteColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
The iPhone reuses old cells with the subview you've added. Try changing the CellIdentifier for each section.
It's because you reuse the cell, but nowhere it is reinitialized.
try this to remove the image :
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
} else {
[cell subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
//you should also reinit the other values, as textLabel, bgColor et al.
}