iphone sdk - select a cell in a tableview, with code - iphone

I would like to select a cell in a tableview, except with code before any user interaction.
Is this at all possible, a quick search through the sdk notes doesnt yield anything. Where should I look?
Regards

UITableView class, "Managing Selections" section.
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition

Related

Event on didSelctItemAtIndexPath when this is into header - iOS

I was building a app on iOS, and in a collectionView I have a header, and needs create event "touch up inside" for this header, like i 've create in the others cells of the collectioView
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
Thx a lot, I hope u help me
It's very easy..
I'm create a simple button and edit in tag attributes -> type -> custom... the button now it's transparent and add a function in event touch up inside.. :D

UITableViewController not highlighting cell when it's selected

Hi
I've got simple question which I don't know how to answer.
In my app I've got UITableViewController with cells. When I select one item (cell) it's getting higlighted and in other thread I'm loading chunk of data to display to the user (after load is done new VC is pushed). When doing it with thread user still can interact with application like, going back to other NavController and I do want that to happen. What I don't want to happen is that when loading isn't complete user can select other cell in table and it get's highlted. How I can prevent that (only highlit, I'm checking if there was a previous request so I'm not putting another thread untli previous request is done).
So basicly my question is, how can you foribd user from interacting with table view controller?
Set the selectionStyle of the UITableViewCell's to UITableViewCellSelectionStyleNone.
You can use the following to check if row can be selected:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (rowSelected) {
return nil;
}
return indexPath;
}
So, you only select it if no row is selected. In your didSelectRowAtIndexPath method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
rowSelected = YES;
// call method that is going to do something and mark rowSelected = NO;
}
You can deselect the row by using
[[tableView cellForRowAtIndexPath:indexPath] setSelected:NO animated:YES];
There is a risk that your users will be confused. A highlight is not enough. There should be very clear visual feedback that a network opperation is ongoing and that different rules apply.
either push the details view immediately after the user selected a row and show an activity indicator in there.
or give the whole table view a different look while loading data for the selected row: e.g. Show activity indicator in the selected row & hide the disclosure chevrons in all the other. While doing that, you can set the selection style to 'none'

How to show advertises in an iPhone App

I write an App that shows all it's information in a UITableView.
Now I use the UIViewController to show 2 UITableViews inside of it.
The first UITableView shows the primary information ( this works fine ).
But now I want to show an advertisement in my App.
I decided to use a second UITableViewController with just one UITableViewCell, which isn't scrollable. ( this second UITableViewController is shown and useable )
The very big question for me is how can I show advertisements there?
I like to show something like a video which I could make with Adobe CS4.
The phone doesn't like Flash, so which format could I use and how can I play it in an UITableViewCell?
Or, when it's better to show it in an UIView or something else, let me know.
Greetz
Wissensdurst
There's an inbuilt ad banner class, and they're also launching a video ad service early next year. Or you could override UITableViewCell to include a video player in it which could play your ad.
Why do you want to use a second TableView Controller? Just us the same and check for the indexPath.row or indexPath.section in you UITableView Delegate Methods. For instance:
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
//show specific informations
}else if(indexPath.row > 0){
//show comercial
}else{
//show all other rows
}
}
or another thing you can use are the tableHeaderView or the tableFooterView. Those views are imageViews so you just can do e.g.
self.tableView.tableFooterView = (UIImageView*)myImageViewWithCommercialBanner;
For you videos, flash is definitely a no go on iphone. Use the required formats in the Apple Documentations and you are fine. For example *.m4v is a commonly used format for this.

iPhone Can't deselect a UITableViewCell

I have a RootViewController class which is inherited from UITableViewController.
When a cell is deselected by the user I want to enable/disable certain buttons on the toolbar.
How do I trap the deselect event?
-(void)tableView:(UITableView *)tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated
{
if(indexPath.row <= rowNumber)
{
[viewButtton setEnabled:NO];
[editButtton setEnabled:NO];
}
}
I tried using this method but it doesn't seem to execute at all. Any ideas how cam this be done?
I do not think there is a deselectRowAtIndexPath event, there is a method that you can call to deselect the indexPath, but looking at the SDK I do not see an event for this in the UITableViewDelegate: http://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html.
Could you enable/disable certain buttons on the toolbar during the didSelectRowAtIndexPath: event?
-Rog
This is in the current beta SDK only which means it could be buggy / changed / unsupported...
I did noticed that your method declaration doesn't match the SDK (at least, the version I have).
Try removing animated:(BOOL)animated; I don't think it's applicable here.
See line ~345 in UITableView.h, and/or right click on didDeselectRowAtIndexPath and "Jump to Definition", where you'll probably find how the delegate method should be defined.
That said, if your goal is simply to "enable/disable certain buttons when a cell is selected",
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
should work just fine. This will occur after they select the cell and before it's deselected. 'deselect' has to do more with animation than user interaction. The only reason I can think you would want to use deselect is maybe the aesthetic value of ensuring your event only occurs after the select cell is no no longer highlighted.

Editing a UITextField inside a UITableViewCell fails

In my application I have a UITextField inside a UITableViewCell. If I click inside the text field and add some text I find that if try to move the insertion point it works the first time but fails on subsequent attempts. I am completely unable to move the selection; no "magnifying glass" appears.
Even more curious, this "setting" seems to be permanent until I restart the application. And it affects all UITextFields on that screen and not just the one that I originally tried to edit.
If you want to see it yourself, try the "UICatalog" sample that comes with the iPhone SDK. Click "text fields" and then "edit" and play around with the text boxes.
I've done a lot of digging on this but it's pretty hard to Google for! The best references I've found are on Apple's support board and MacRumors formum (both reference a solution that apparently used to work on iPhone 2.0 but does work not with contemporary versions -- I did try).
My feeling that is that this is a bug in the OS, but I thought I'd throw this out to the SO crowd for a second opinion and to see if there are any workarounds. Any ideas?
Following benzado's suggestion, I tried building my application using the 2.0, 2.1 and 2.2 SDKs. I got the same behaviour in all versions. (Actually, something related but not the same broke in 2.2 but that's probably another question!)
I spent a lot of time on this but I finally think that I have it nailed.
The trick is that the table needs to be editable (i.e., its editing property needs to be set to YES). The good news is that you are now able to move the insertion point. Sometimes the magnifying glass doesn't appear or follow but your gesture always seems to work.
Does this still qualify as a bug? Perhaps. At the very least Apple's SDK documentation should be updated. I've raised a bug report with Apple to cover this (rdar://6462725).
Thanks to this post, I've been able to successfully get this to work properly in my app.
Something to add, however:
If you set your table to be editable, you'll likely get different behavior than you expect (indenting, editing widgets, no disclosure indicators, etc.). This surprised me but here's how to best deal with it:
In your UITableView delegate, implement:
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
Then, in your UITableViewCell's implementation, set your UITableView to be editable ONLY when you're actually editing:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
((UITableView *)[self superview]).editing = YES;
...
}
and disable editing when editing is done:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
...
((UITableView *)[self superview]).editing = YES;
}
This will ensure that your table isn't in editing mode when you're not editing the cell, keeping things working smoothly.
Thanks!
Brian M. Criscuolo
Mark/Space Inc.
The answer by Brian M. Criscuolo is the closest, however its still not quite right - in my usage of SDK2.2.1 I find that I have to do the following:
To your UITableViewDelegate (which is often your UITableViewController) add both of the following:
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
and:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
and:
- (void)viewDidLoad {
[super viewDidLoad];
// do any other customisation here
self.uiTableView.editing = true;
}
If you don't put the top two delegate methods, the above will cause the delete icons next to each row, and the indentation of each row.
You shouldn't need to do anything with textfield delegates as Brian indicated (unless you have multiple rows and you want to respond to a didSelectRowAtIndexPath: event - which you don't seem to get while in edit mode - then you will need to also do as he suggests).
By the way - this seems fixed in SDK3.0 (although subject to change I guess)
It does sound like an OS bug. I would try to reproduce it by running the UICatalog sample against the 2.0, 2.1, and 2.2 SDKs to see if anything changes. (There's a bug related to table cell text alignment that occurs if you build for 2.2 but not if you build for 2.1, regardless of what version of the OS is on the device.)
If it turns out to make a difference, https://bugreport.apple.com/
I tried this with my application and it seems to work as expected. I get a magnifying glass every time. I am using the 2.1 SDK.
actually what works best seems to be to set the table "editing" property to true in "viewDidLoad" and adding these to the table delegate
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
you don't need to do anything in the text field delegate
In iOS 12 or later you can select table view cell in the storyboard, then enable User Interaction Enabled in the attribute inspector.