iPhone: Use UITableViewCellStyleValue1 and UITableViewCellStyleValue2 in same tableView - iphone

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...

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.

How to Select & checkmark the row after load in UITableview

I have a selection that will select just 1 row of category.
but I will like it to select Spirits row when loaded.
Something like this:
At the moment it comes to this without selecting anything:
Where shall I do the compare for the in order for it to selectRowAtIndexPath?
// 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:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
categoryString = [arrayCategory objectAtIndex:indexPath.row];
cell.textLabel.text = categoryString;
if (cell.textLabel.text == categoryString) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* newCell = [tableView cellForRowAtIndexPath:indexPath];
int newRow = [indexPath row];
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
if(newRow != oldRow)
{
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
newCell.highlighted =YES;
UITableViewCell* oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = indexPath;
oldCell.highlighted = NO;
}
[tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES];
}
Use selectRowAtIndexPath:animated:scrollPosition: method to select a row programatically.
Few things to correct in your code. You should use isEqualToString: method to match the strings, like this, if ([cell.textLabel.text isEqualToString:categoryString]) {. Next thing is, you are assigning the categoryString to cell.textLabel.text, and on the next line you are matching them, so it would always return YES. I think you are matching the wrong values.
You have to manually set multiple check marks. for that you can use UIImageview for each UITableViewCell and as per the stored old data, you have to set check marks in didSelectRowAtIndexPath(delegate method of UITableView).

How to use UITextAlignment to align text in UITableViewCell to the right and left in Swift

I have to align text in table cell to right and left.I have used two plist in my application
according to plist selection I have to align my text in table cell.
I try following code for that
if ([app.currentPlist isEqualToString:#"Accounting.plist"]) {
cell.textAlignment=UITextAlignmentLeft;
} else {
cell.textAlignment=UITextAlignmentRight;
}
UITableViewCell doesn't have a textAlignment property. You have to set it on the cell's text label:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = #"Something";
if([app.currentPlist isEqualToString:#"Accounting.plist"])
{
cell.textLabel.textAlignment=UITextAlignmentLeft;
}
else
{
cell.textLabel.textAlignment=UITextAlignmentRight;
}
return cell;
}
UITextAlignmentRight is deprecated, use NSTextAlignmentLeft instead
So, the code will be -
cell.textLabel.textAlignment = NSTextAlignmentLeft;
In swift it is
cell.textLabel.textAlignment = NSTextAlignment.Right

Apply different cell properties depending on section

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 ...

Iphone - cellForRowAtIndexPath behaving weirdly when scrolling down and up

I would like to get a TableView with different elements on the accessoryView, depending on the section. Everything works fine, but when I scroll down to section 2 and then scroll up again to section 0, the rows in section 0 have switchers. Could you please help me finding out what I'm doing wrong?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger sectionIndex = [indexPath section];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *dictionary;
NSArray *array;
NSString *cellValue;
UISwitch *switchView;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
switch (sectionIndex) {
case 0:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
case 1:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
case 2:
switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = switchView;
[switchView setOn:NO animated:NO];
[switchView addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[switchView release];
break;
default:
break;
}
dictionary = [searchArray objectAtIndex:indexPath.section];
array = [dictionary objectForKey:#"advancedSearch"];
cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
You are dequeuing your table cells (cf. -[UITableView dequeueReusableCellWithIdentifier:]), which means that you are receiving table views that have already been allocated and possibly pre-populated with stale data. In case 0: and case 1: of your switch statement, you should add the following line:
cell.accessoryView = nil;
Use different cell identifiers for different cells.
Something like:
NSString *CellIdentifier;
switch ([indexPath row]) {
case 0: CellIdentifier = #"cell0";
break;
case 1: CellIdentifier = #"cell1";
break;
default: CellIdentifier = #"cell";
break;
}
Otherwise Xcode thinks that all your cells have the same look and tries to reuse them while you scroll.
The problem is from dequeueReusableCellWithIdentifier you must use different identifiers for different cells because you reuse them.