Apply different cell properties depending on section - iphone

following code works properly but it applies same cell text to all sections. How could I apply selection case also depending on section? Thank you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
switch (indexPath.row) {
case 0:
cell.textLabel.text = #"English";
break;
case 1:
cell.textLabel.text = #"Chinese";
break;
case 2:
cell.textLabel.text = #"Japanese";
break;
default:
break;
}
return cell;
}

You can use indexPath.section to get the section number, just like how you're using indexPath.row for the row number.

You have to check for the indexPath.section and also indexPath.row:
if (indexPath.section == 0) {
if (indexPath.row) {
...
}
} else if (indexPath.section == 1)
if (indexPath.row) {
...
}
} else if ...

Related

UITableView selected cell not displaying text when started

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.

Reset UItableViewCell checkmarks

following code starts with a blank cells and add a checkmark when selected cell but, once added, if you press again that cell, it is not erased. I am trying to reset it each time that didSelectRowAtIndexPath is called. How to clear cell in order to be marked only one? Thanks.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
labelInfo.text=#"1";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 1:
labelInfo.text=#"2";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 2:
labelInfo.text=#"3";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 3:
labelInfo.text=#"4";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 4:
labelInfo.text=#"5";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
default:
break;
}
}
}
This code will remove all checkmarks on the table
[[self.tableView visibleCells] makeObjectsPerformSelector:#selector(setAccessoryType:) withObject:UITableViewCellAccessoryNone];
Just copy-paste this. It works.
//in your .h file
int selectedCell;
//in your .m file
- (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 == selectedCell)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.selected = YES;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selected = NO;
}
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedCell = indexPath.row;
[tableView reloadData];
//remaining code.
}
Try this:
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
In your code you start with
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
Which remove check mark from selected cell, but later you assign checkmark again with
switch (indexPath.row) {
case 0:
labelInfo.text=#"1";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 1:
labelInfo.text=#"2";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 2:
labelInfo.text=#"3";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 3:
labelInfo.text=#"4";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 4:
labelInfo.text=#"5";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
default:
break;
}
So, you can use something like
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
for toggle the checkmark and
labelInfo.text = [NSString stringWithFormat:#"%d", indexPath.row + 1];
for label
do this:-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
labelInfo.text=#"1";
if (cell.accessoryType==UITableViewCellAccessoryCheckmark)
cell.accessoryType=UITableViewCellAccessoryNone;
else
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 1:
labelInfo.text=#"2";
if (cell.accessoryType==UITableViewCellAccessoryCheckmark)
cell.accessoryType=UITableViewCellAccessoryNone;
else
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 2:
labelInfo.text=#"3";
if (cell.accessoryType==UITableViewCellAccessoryCheckmark)
cell.accessoryType=UITableViewCellAccessoryNone;
else
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 3:
labelInfo.text=#"4";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 4:
labelInfo.text=#"5";
if (cell.accessoryType==UITableViewCellAccessoryCheckmark)
cell.accessoryType=UITableViewCellAccessoryNone;
else
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
default:
break;
}
}

How to display different element from different array in different section in single table view?

I have a table with 3 sections, all three sections have different number of rows. The cell values should populate from different array's.
So how should i find out which section is it in method cellForRowAtIndexPath:???
Please help!!!
Code:
- (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section
{
currentSection = section;
int number = 0;
switch (section) {
case 0:
number = self.basic.count;
break;
case 1:
number = allSectors.count;
break;
case 2:
number = 1;
break;
default:
number = 0;
break;
}
return number;
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]autorelease];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
CGFloat fontSize = [UIFont systemFontSize];
CGFloat smallFontSize = [UIFont smallSystemFontSize];
switch (indexPath.section) {
case 0:
cell.textLabel.text = [self.basic objectAtIndex:indexPath.row];
break;
case 1:
cell.textLabel.text = [self.allSectors objectAtIndex:indexPath.row];
break;
case 2:
cell.textLabel.text = #"None";
break;
default:
break;
}
return cell;
}
Just use indexPath.section to get the table section, and indexPath.row to get the row in that section. These properties are defined in the category NSIndexPath (UIKitAdditions).

iPhone: Use UITableViewCellStyleValue1 and UITableViewCellStyleValue2 in same tableView

What is the best way to use both UITableViewCellStyleValue1 and UITableViewCellStyleValue2 in same tableView? I am not sure the best way to do this, since I initWithStyle before my switch:
// 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:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
cell.textLabel.text = #"Username";
break;
case 1:
cell.textLabel.text = #"Password";
break;
}//end switch
} else if (indexPath.section == 1) {
switch (indexPath.row) {
case 0:
cell.textLabel.text = #"Create Account";
break;
}//end switch
}
return cell;
}//end
How can I use UITableViewCellStyleValue2 in my "Create Account" cell?
Why don't you move initWithStyle inside your switch? If you're trying to save some room and you need Value2 rare comparing to Value1, then you can leave initWithStyle before switch, and then create it again inside switch for cells of Value2 style.
Determine the cell style and identifier before calling initWithStyle.
You need to use a different cell re-use identifier for each style because if a cell with style Value1 gets de-queued, you don't want to re-use it for a row that needs style Value2.
static NSString *CellIdentifierValue1 = #"CellIdentifierValue1";
static NSString *CellIdentifierValue2 = #"CellIdentifierValue2";
//default settings...
NSString *reuseIdentifier = CellIdentifierValue1;
UITableViewCellStyle cellStyle = UITableViewCellStyleValue1;
if ((indexPath.section == 1) && (indexPath.row == 0))
{
//special settings for CreateAccount...
reuseIdentifier = CellIdentifierValue2;
cellStyle = UITableViewCellStyleValue2;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:cellStyle reuseIdentifier:reuseIdentifier] autorelease];
}
//rest of existing code...

UITableView not updating correctly when scrolling

I am writing a UITableView that contains 2 sections. When the table first loads all the cells are displaying the correct info, but when I start scrolling up and down, cells detailedTextLabel and accessoryType are being refreshed incorrectly, such that some cells that should only contain a detailedTextLabel also contain an accessory, and cells that should only contain an accessory also contain a detailedTextLabel.
Inside cellForRowAtIndexPath: I am using nested switch/case statements to apply the correct values to the cells in their respective section/row. As far as i can tell the logic in these statements is correct, so is it possible that the value of cell variable is inccorect when updating?
The table loads correctly but after scrolling accessoryType and detailedTextLabel gets mixed up.
Click for link to screen shots of the table.
Here is the code inside of my UITableViewController subclass:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [sectionNames count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSArray *headingsSection = [cellTitles objectAtIndex:section];
return [headingsSection count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [sectionNames objectAtIndex:section];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
self.tableView.allowsSelection = YES;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [[cellTitles objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
switch (indexPath.section) {
case 0:
cell.detailTextLabel.text = [NSString stringWithFormat:#"%d%%", [[assistSettingsArray_glob objectAtIndex:indexPath.row] intValue]];
break;
case 1:
switch (indexPath.row) {
case 0:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
case 1:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
case 2:
if (defaultAssistOn) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
break;
}
break;
}
return cell;
}
Due to cell re-use, cells with previously set values re-appear.
Before the switch (indexPath.section) ..., initialize the detailTextLabel and accessoryType to defaults:
cell.detailTextLabel.text = #"";
cell.accessoryType = UITableViewCellAccessoryNone;
Try to enclose the code in each switch cases inside parantheisis. So the above code looks like this:
switch (indexPath.section) {
case 0: {
cell.detailTextLabel.text = [NSString stringWithFormat:#"%d%%",
[[assistSettingsArray_glob objectAtIndex:indexPath.row] intValue]];
break;
}
case 1: {
switch (indexPath.row) {
case 0: {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break; }
case 1: {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break; }
case 2: {
if (defaultAssistOn) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
break; }
default:
break;
}
break;
}
default:
break;
}
I am not sure whether this work. Do inform if it does :)