How can you turn the blue selector to go off when I move back into the view? At the moment, when I select it, I get pushed to another view. However, when I go back into the original view, it is still selected.
How can I turn it off when I go back onto the original view?
Thanks.
In your tableView:didSelectRowAtIndexPath: method you should include the following:
[tableView deselectRowAtIndexPath:indexPath animated:YES];
You can also try this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"myCellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Set up the cell...
cell.textLabel.text = #"foo";
return cell;
}
I "always" deselect the rows when the view disappears:
-(void)viewDidDisappear:(BOOL)animated {
int i = 0;
while (YES) {
NSIndexPath *path = [NSIndexPath indexPathForRow:i++ inSection:0];
if ([yourTable cellForRowAtIndexPath:path] == nil) { break; }
[yourTable deselectRowAtIndexPath:path animated:NO];
}
}
Haven't checked the code
Related
I need to literally redraw a UITableView upon an event in my code. But everything I've tryed doesn't seem to work. I've tried [self.tableView reloadData], I've played with the delegates. The goal I'm trying to achieve is to render a completely different UITableView, with differently formatted cells. So I need to redraw the table. Any help would be appreciated.
Heres my code:
...
if/else...
}
//Now I want to reload the tableView
[tableView reloadData]; //Isn't getting it done
NSLog(#"index = %i", index); //Always fires
tableView.delegate = self; // Didn't help
[tableView performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO]; // Also nothing
}
The point of what I'm trying to do is this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
if (segmentOptions == snacks) {
cell = [[TableViewCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSLog(#"A");
} else {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
NSLog(#"B");
}
}
...
}
You possibly meant something like
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"cell-not-snacks";
if (segmentOptions == snacks) {
cellIdentifier = #"cell-snacks";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
if (segmentOptions == snacks) {
cell = [[TableViewCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
} else {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
}
}
I think it would be worth looking at the method prepareForReuse in the UITableViewCell Class. This method is invoked just before the object is returned from the UITableView method dequeueReusableCellWithIdentifier:.
You can override this method on your custom UITableViewCell (do not forget including [super prepareForReuse]) in order to redraw whatever you need to redraw.
The problem is: if (cell == nil) {. I removed that and it works perfectly every time.
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).
I have created a custom table for the rootViewController of a split view application so that the selected row expands and shows as a sub-table (showing main menu and sub menu ). The first row of the sub-table should show the particular main menu item of the sub-table.I'm fetching the main menu items from another class.
The problem is that the first row for the sub table is showing blank for me.Using NSLog, I checked the value of variable just before assigning it to the cell and even after assigning the value to the cell, i checked the text value in the cell using cell.textLabel.text. I'm getting the value in the console every time, but the row is still blank!!!
Row is showing the value if I'm hard coding it with any value!!!
Note:TableView is showing values for remaining rows.
Anybody can help me?? Thanks in advance...and sorry for my poor English..
EDIT: In the rootViewController:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
tableView.separatorColor=[UIColor grayColor];
if (sectionopen[indexPath.row]) {
accordianTable *cell1;
cell1=(accordianTable *)[tableView dequeueReusableCellWithIdentifier:#"cell1"];
if (cell1 == nil) {
cell1 = [[[accordianTable alloc] initWithFrame:CGRectZero reuseIdentifier:#"cell1"] autorelease];
}
cell1.selectionStyle=UITableViewCellSelectionStyleNone;
return cell1;
} else {
//tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
static NSString *CellIdentifier = #"CellIdentifier";
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
// Configure the cell.
cell.textLabel.text=[UIAppDelegate.mainMenu objectAtIndex:indexPath.row];
return cell;
}
}
(void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
accordianTable *acc = [[accordianTable alloc]init];
acc.titl=[UIAppDelegate.mainMenu objectAtIndex:indexPath.row];
[acc.subTable reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
///turn them all off
sectionopen[0]=NO;
sectionopen[1]=NO;
sectionopen[2]=NO;
sectionopen[3]=NO;
///open this one
sectionopen[indexPath.row]=YES;
///animate the opening and expand the row
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
UIViewController *localdetailViewController = nil;
}
In the custom cell class (accordianTable):
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Number of rows is the number of time zones in the region for the specified section.
return [UIAppDelegate.subMenu count]+1;//including title and sub menu
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
switch (indexPath.row)
{
case 0:
NSLog(#"text is >> %#",titl);
cell.textLabel.text=titl;
NSLog(#"text is >> %#",cell.textLabel.text);
cell.textLabel.textColor=[UIColor whiteColor];
cell.contentView.backgroundColor=[UIColor blackColor];
cell.textLabel.backgroundColor=[UIColor blackColor];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
break;
default:
int Row=indexPath.row;
Row--;
cell.textLabel.text=[UIAppDelegate.subMenu objectAtIndex:Row];
cell.textLabel.textColor=[UIColor orangeColor];
cell.textLabel.textAlignment=UITextAlignmentCenter;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
break;
}
return cell;
}
for some reason this UITableView keeps crashing and it seems like the UITableView is unable to display the secondsString for some reason but i have no idea why... please help me out here cause i have no idea what i could be doing wrong..... Thanks!!
#import "SettingsView.h"
#implementation SettingsView
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
transmissionSetup = [[TransmissionSetupViewController alloc] initWithNibName:#"TransmissionSetupViewController" bundle:nil];
transmissionSetup.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
NSLog(#"%i", [self getMinutes]);
[self setSeconds:10];
secondsString = [NSString stringWithFormat:#"%i", [self getSeconds]];
[self.tableView reloadData];
}
#pragma mark Table view methods
// 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) {
cell.textLabel.text = #"Every:";
cell.detailTextLabel.text = secondsString;
}
// Set up the cell...
return cell;
}
#end
You initialize your secondString with autoreleased string - so it is not guaranteed that it will be valid outside current scope. You need to retain it upon initialization (and don't forget to release later)
I would advise you to have a look at objective-c properties for accessing ivars.
// try this
- (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];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.section == 0) {
cell.textLabel.text = #"Every:";
cell.detailTextLabel.text = secondsString;
}
// Set up the cell...
return cell;
}
I have a UITableView in Group Style, and I am using it with Navigation Controller.
When a user click on the cell I am pushing to another view for a user to make selection, while all that is working fine. I want the user to be brought back to the first view when the user makes a selection. and I want to display their selection on the cell.
Thanks in advance for your help
I think you can use the NSUserDefaults.
// view controller to make selection
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selected;
if (indexPath.row == 0) {
selected = #"Apple";
} else if (indexPath.row == 1) {
selected = #"Microsoft";
}
[[NSUserDefaults standardUserDefaults] setObject:selected forKey:#"SELECTED"];
[self.navigationController popViewControllerAnimated:YES];
}
Important point is you need to call [self.tableView reloadData] in viewDidAppear.
// view controller to show what a user selected
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.tableView reloadData];
}
- (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];
}
// Configure the cell.
cell.textLabel.text = #"Choice";
cell.detailTextLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:#"SELECTED"];
return cell;
}