I'm working with a customized TableView and I have a problem when I click on a cell and it gets highlighted. After clicking on the cell, it gets highlighted and takes me to another view. The problem is that, when I return to the TableView screen, the cell is still highlighted.
Here is my code:
In cellForRowAtIndexPath
cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage
imageNamed:#"CustomCell.png"]] autorelease];
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"CellHighlighted.jpg"]] autorelease];
In didSelectRowAtIndexPath
Characteres *vCharacters = [[Characters alloc] initWithNibName:#"Characters" bundle:[NSBundle mainBundle]];
//vCharacters.selectedCountry = selectedCountry;
[self presentModalViewController:vCharactersr animated:YES];
[vCharacters release];
vCharacters = nil;*
You need to add this to didSelectRowAtIndexPath:
[tableView deselectRowAtIndexPath:indexPath animated:NO];
This will deselect the row.
Related
In my iphone app, I first call a controller which stack up a few view controllers based on index. This is done cause based on the user's selection, i will need to show different screens (basically, i have a welcome screen, tabbar view - which is the main app, sign in and sign up pages). As of now, everything works perfectly - I stack them up and remove / switch based on the need. The problem is that i would like to add a nav bar to both the sign in and sign up views. However, when i do that, something weird happens - i see a nav bar, but on TOP of it, there is a white bar as well (half width more or less). How can i add this uinavbar successfully?
Here is the controller that is being called by the AppDelegate:
- (void)viewDidLoad
{
[super viewDidLoad];
myTabBarVC = [[tabBarController alloc] initWithNibName:#"tabBarController" bundle:nil];
[self.view insertSubview:myTabBarVC.view atIndex:0];
myLoginVC = [[loginViewController alloc] initWithNibName:#"loginViewController" bundle:nil];
[self.view insertSubview:myLoginVC.view atIndex:1];
mySignUp = [[SignUpView alloc] initWithNibName:#"SignUpView" bundle:nil];
[self.view insertSubview:mySignUp.view atIndex:2];
myWelcomeView = [[WelcomeView alloc] initWithNibName:#"WelcomeView" bundle:nil];
[self.view insertSubview:myWelcomeView.view atIndex:3];
}
When I add that to one of the view controllers in this same method, it doesnt work, as described above.
myLoginVC = [[loginViewController alloc] initWithNibName:#"loginViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myLoginVC];
[self.view insertSubview:navController.view atIndex:1];
How can I make it work? Please help. Thanks!
ADDING MORE INFO ABOUT THE VIEW WHERE THE KEYBOARD IS ON BY DEFAULT:
- (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:#"Cell"];
cell.textLabel.text = [[NSArray arrayWithObjects:#"",#"",nil]
objectAtIndex:indexPath.row];
if (indexPath.row == 0) {
textField1 = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 280, 21)];
textField1.delegate = self;
textField1.placeholder = #"example#EMAIL.com";
textField1.text = [inputTexts objectAtIndex:indexPath.row/2];
textField1.tag = indexPath.row/2;
cell.accessoryView = textField1;
textField1.returnKeyType = UIReturnKeyNext;
textField1.clearButtonMode = UITextFieldViewModeWhileEditing ;
[textField1 becomeFirstResponder];
textField1.tag = 1;
}
else
{
textField2 = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 280, 21)];
textField2.delegate = self;
textField2.placeholder = #"password";
textField2.text = [inputTexts objectAtIndex:indexPath.row/2];
textField2.tag = indexPath.row/2;
textField2.returnKeyType = UIReturnKeyDone;
textField2.clearButtonMode = UITextFieldViewModeWhileEditing;
cell.accessoryView = textField2;
[textField1 becomeFirstResponder];
textField1.tag = 2;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
I don't think you should be adding navController.view as a subview to your view controller. I suspect that's a source of the trouble. Instead use a UINavigationBar object.
With regards to your second question, if it was a question and not a remark, about the keyboard being by default, that happens because you set your textField1 as first responder.
I have a main table view and a detail view. when i click cell detail view should show the details of that item. now what i wanna do is to create subview and put that subview in detailView instead of creating a detailview for every cell. it ll solve my some other problems. Code looks like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *nextController = [[DetailViewController alloc] init];
int storyIndex = [indexPath indexAtPosition:[indexPath length] -1];
[nextController initWithObjectAtIndex:storyIndex inArray:stories];
NSString *storyTitle = [[stories objectAtIndex:storyIndex] objectForKey:#"title"];
nextController.title = #"Details";
UIBarButtonItem *tempButtonItem = [[[UIBarButtonItem alloc] init] autorelease];
tempButtonItem.title = #"Tillbaka";
self.navigationItem.backBarButtonItem = tempButtonItem ;
[self.navigationController pushViewController:nextController animated:YES];
[nextController release];
}
now how can i create a subview with a label and change text of that label to storyTitle.
thanx in advance.
how can i create a subview with a label and change text of that label to storyTitle
Given a parent view, you don't need to create a new subview just to add a label to the parent. But, assuming you really do want to add a subview with a label to your parent view, then do this:
...
UIView *parentView;
...
UIView *subView = [[[UIView alloc] initWithFrame:mySubViewFrame] autorelease];
UILabel *lbl = [[[UILabel alloc] initWithFrame:myLabelFrame] autorelease];
lbl.text = storyTitle;
[subView addSubview: lbl];
[parentView addSubview: subView];
i want to display the table view in alert view. This table view contains segmented control.
these segmented controls are for on/off the audio, image, text. so there will be 3 cells with segmented controls.
how to do this
please help me out
Thank u
Adding UISegmentedControls (or UISwitches, which I would personally recommend for a simple on/off option) is easy enough to put in table view cells using the accessoryView property.
Your view controller (or whatever object you're using to control this thing) must implement the UITableViewDataSource protocol.
#interface MyViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> {}
#end
Add the controls in tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* const SwitchCellID = #"SwitchCell";
UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:SwitchCellID];
if( aCell == nil ) {
aCell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SwitchCellID] autorelease];
aCell.textLabel.text = [NSString stringWithFormat:#"Option %d", [indexPath row] + 1];
aCell.selectionStyle = UITableViewCellSelectionStyleNone;
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
aCell.accessoryView = switchView;
[switchView setOn:YES animated:NO];
[switchView addTarget:self action:#selector(soundSwitched:) forControlEvents:UIControlEventValueChanged];
[switchView release];
}
return aCell;
}
Or, if you're set on segmented controls, replace the UISwitch stuff above with something like:
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"On", #"Off", nil]];
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.frame = CGRectMake(75, 5, 130, 30);
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl addTarget:self action:#selector(controlSwitched:) forControlEvents:UIControlEventValueChanged];
aCell.accessoryView = segmentedControl;
[segmentedControl release];
I would personally put these options in a regular view (probably accessible from an options button which does the flip transition, like many iPhone apps do). I don't think putting it in an alert view is a very good user experience.
However, I have written a few blog posts showing how to put various views into alert views (text field, which is kinda useful, and web view, which is arguably not).
So, if you're really dead set on putting this in an alert view, you can totally do that, like this:
- (void) doAlertWithListView {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Preferences"
message:#"\n\n\n\n\n\n\n"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"OK", nil];
UITableView *myView = [[[UITableView alloc] initWithFrame:CGRectMake(10, 40, 264, 150)
style:UITableViewStyleGrouped] autorelease];
myView.delegate = self;
myView.dataSource = self;
myView.backgroundColor = [UIColor clearColor];
[alert addSubview:myView];
[alert show];
[alert release];
}
It will look something like this:
The answer above works great, but on the iPad the line myView.backgroundColor = [UIColor clearColor];
doesn't seem to remove the grey rectangular area around the table. I find that the following does the job: tableView.backgroundView.alpha = 0.0;
How to change cell background image on click on cell in iphone????I have tried following but it is not working....
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,320,50) reuseIdentifier:myid] autorelease];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myImage.png"]];
}
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myImage.png"]];
In the above piece of code you forget to autorelease; also have you set the delegate of the tableview? Set a breakpoint on the tableview:didselectRowAtIndexpath method to check if it is called or not.
BTW your code seems right.
You can change the selectionType of UITableView
using
cell.setSelectionStyle = [UITableViewSelectionStyleGray/Blue/None] (only 3 types)
some syntex error in above code try with proper syntex
by default it will be blue...
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"mImage.png"]]
I have a UITextView within a UITableView cell. I have been unable to get the keyboard to consistently resign after editing. Detecting DidEndEditing hasn't worked. Adding my own "done" button to the toolbar brings intermittent results. Advice?
(Note: This is UITextView not UITextField. Thanks)
Do you dismiss the table view's controller after you're done editing? I've encountered a non-deterministic crash that occurred when performing [textView resignFirstResponder] plus a call (something like [self doneClicked:nil]) that would dismiss the view controller that hosted the UITableView.
It would release the UITextView and when the call came back into the UITextView method that originated the didEndEditing call, it would crash or behave inconsistently (since the view had been released)..
The solution was to call everything after some delay:
[self performSelector:#selector(doneClicked:) withObject:nil afterDelay:0.5]
adding the textview to the cell:
cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
managedTextView = [[[UITextView alloc] initWithFrame:CGRectMake(7,8,260, 30)] autorelease];
managedTextView.delegate = self;
managedTextView.scrollEnabled = YES;
managedTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
managedTextView.text=thought.managedthought;
[cell.contentView addSubview: managedTextView];
cell.accessoryType = UITableViewCellAccessoryNone;
done button code:
- (void)saveTextView:(id)sender
{
[managedTextView resignFirstResponder];
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(save:)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];
...
}
(the "new" save button is used when saving the entire UITableViewController)