yesterday I just asked following question.
How to customize tableView Section View - iPhone
I found some new method.
Even in apple documentation I didn't found this method.
Is it some thing like hidden methods?
Does anybody provides all methods listing?
Including sample code.
Say for example.
UITableView methods
Whenever I insert tableView in my view Controller.
I have to either type or copy from some where.
If I want to include picker I have to find out UIPicker methods,
sameway
Alertview, ActionSheet, Tab Bar Controller all has different methods.
Isn't it possible, like if we include A tableView in our ViewController, Automatically all tableview methods are added to .m file.
(For example, A navigation based application has all tableView methods in rootview controller by default)
Let Me Clarify Again,
"I need proper source where all methods (like rootview controller has almost all table methods) "
So, when ever I want to add any control I just copy the code & add to my Project.
The reason Behind this
"We can target on the work instead of finding proper methods & typing them."
See, Suppose If I add a Table View to my View Controller, I must have the methods like ..didSelectAtRow..,..CellForRow...,etc.
So, after adding tableView - for managing table view I have to go for finding methods & type them in my .m file.
Suppose, I add tableView. All methods should be added to my .m file as given below.
<pre>
pragma mark Table view methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 0;
}
-(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];
}
return cell;
}
// Override to support row selection in the table view.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here -- for example, create and push another view controller.
// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:#"AnotherView" bundle:nil];
// [self.navigationController pushViewController:anotherViewController animated:YES];
// [anotherViewController release];
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
I provided the answer to the question you mentioned - it definitely is in the Apple documentation (although as you say, not in the sample file). Remember the method name is
tableview:didSelectRowAtIndexPath
if you miss off the "tableview:" bit at the beginning and just search for
didSelectRowAtIndexPath
you won't find it in the documentation so easily.
If you look in the documentation that comes with XCode, you will see, for example, all methods that you can implement for the UITableview Delegate, including the one I posted to your previous answer. Just type "UITableview" into XCode help, and then select "UITableview delegate". It will then display all the methods available for you to call, and you can even just copy and paste them straight into your code.
I don't know if anyone's already done this and made the "template" classes you ask about available, but it should be very easy for you to do this yourself if you want.
Hope that helps
Sure; implementors of classes are free to implement any number of methods as a part of a class's internal implementation.
But that doesn't mean that you should use them.
You can use the Objective-C runtime's API for figuring out all the methods and classes, including those that aren't publicly declared.
But don't bother.
Specifically, if a method is not declared in the provided header files and is not documented in the documentation, don't use it. Using such a method will lead to fragility and maintenance headaches; your app may likely break at the next software update.
On the iPhone, your are expressly directed not to use private interfaces and your app will run the risk of rejection if you do so.
But I don't think that is what you are really asking. You mention:
Say for example. UITableView methods
includes
didSelectRowAtIndexPath
cellForRowAtIndex Path
numberOfSectionsInTableView
titleForHeaderInSection
However, UITableView does not declare any of those methods. Instead, it declares:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
Or, succinctly, tableView:didSelectRowAtIndexPath:, etc...
The methods you describe are in the documentation. They're in UITableViewDelegate and UITableViewDataSource, which are described at the top of the UITableView documentation. You can easily copy and paste the method definitions you need from the documentation. You can also find the protocol definition in the headers easily by using "File>Open Quickly..." and typing in the name of the protocol ("UITableViewDelegate" for instance). They are often written in the headers to make it easy to copy and paste what you need most often.
This is sometimes a small hassle in Cocoa, because Xcode doesn't auto-complete method signatures. It would save a little trouble if it did. But the solution is not to implement every delegate method that exists (as #bbum pointed out earlier). In the vast majority of cases, only a small fraction of the possible delegate methods are ever implemented. So automatically populating them all would cause much more work than it saved.
Related
I have a splitview controller and i'd like to manage more than one table in the detail view (using this cocoa control, for each section one table). Should I use 4 specific view controller or just one that controls a subview?
As per documentation :
A UITableView object must have an object that acts as a data source
and an object that acts as a delegate; typically these objects are
either the application delegate or, more frequently, a custom
UITableViewController object. The data source must adopt the
UITableViewDataSource protocol and the delegate must adopt the
UITableViewDelegate protocol. The data source provides information
that UITableView needs to construct tables and manages the data model
when rows of a table are inserted, deleted, or reordered. The delegate
provides the cells used by tables and performs other tasks, such as
managing accessory views and selections.
I would suggest to use four different UIViewController that will modularise your code and easy to managing as compared to every thing in a single UIViewController.
You can use number of table view in single base view, but you need to handle it with some tag…like tag value (any integer value) or by name. I think you need to manage with tag and this will happens like below:
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView.tag == ) {
return value;
}
}
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView.tag == ) {
return value;
}
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView.tag == ) {
<#statements#>
}
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
return cell;
}
But in this scenarios you need to handle entire condition gently. And always care about that do not code large statement inside cellForRowAtIndexPath, another wise it will take so much time to initialize that cell at the loading time.
I'm stuck with a seemingly very simple app that I'm trying to develop
Basically, I have a UITable, and two buttons: red/blue
When a button is pressed, a row with corresponding title of that button is append to the table
I'm overwhelmed by how complicated UITableView has to be implemented (datasource, delegate, resuable identifier, etc)
Can anyone help me out with this, preferably show me detailed codes
For my Buttons, I have something like this
- (IBAction)buttonPressed:(UIButton *)sender{
NSString *item = sender.currentTitle;
[self.cellArray addObject:item];
[self.myTable reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.cellArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"The Table Cell";
self.myTableCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (self.myTableCell == nil) {
self.myTableCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
self.myTableCell.textLabel.text = [self.cellArray objectAtIndex:indexPath.row];
return self.myTableCell;
}
Since you are learning this..i will post a simple solution.
Make add a member variable NSMutableArray *cellArrays;
initialize it in your viewDidLoad
in buttonPressed check;
if([#"red" isEqualToString:[YourButton titleForState:UIControlStateNormal]])
{
[self.cellArrays addObject:#"red"];
}
else
{
[self.cellArrays addObject:#"blue"];
}
[self.YOURTABLEVIEW reloadData];
Now in your table view datasource method
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.cellArrays.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//in between your code;
YOURCELL.textlabel.text = [self.cellArrays objectAtIndex : indexpath.row];
}
Try this ..
I'd recommend subclassing UITableViewController, then override the dataSource and delegate methods and you should be good to go. Going straight to UITableView is more complicated without any obvious benefits.
You may not be using Core Data, but I still recommend this lecture because it hooks up a TableViewController and works great - all code included. Check it out:
http://www.stanford.edu/class/cs193p/cgi-bin/drupal/node/289
The lectures in iTunes U explain everything further.
Enjoy,
Damien
I wanted to add that "One more thing, the compiler shows a warning says no reusable identifier. what exactly is that?" above...
His answer is correct, but to get rid of that compiler warning...
The tableviewcell of your tableview has to have an identifier. In IOS 5, in your storyboard, highlight the TableViewCell in your TableView, and enter a value in the Identifier. This must match the value in your code of the cell that you are creating.
I've searched around and I can't seem to figure out how to do this. It doesn't help that I don't really know what I am totally doing yet, but I hope this will help.
I am creating an iPad application. In short it is a complex stopwatch that will take splits (for running) on one view.
I have a master clock, and 5 buttons for separate splits. All that works. But, I want to record these splits and I thought it would be great to do it in a table that can be scrolled through.
I have 5 UITableViews on the one view. I found some stuff online for a "datasource protocol" and got everything working great for just one table. Things went to crap when I tried to make it work separately for each table. Also, it seemed like a ton of code for a simple task.
I have 5 mutable arrays already present. I really don't know how to go about this and any help would be great!
Also, if possible, i need to clear the tables with the press of a button...seems simple, but I truly don't know.
Thanks!
You need to set UITableViewDelegate and UITableViewDataSource for all your tables to a class that will implement the following methods:
For UITableViewDataSource:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
and
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
As these methods are passed the calling tableview, it should be easy for you to return the correct data for the tableview in question (which you are already storing in an NSMutableArray). You still need to cater for displaying different content for your arrays, but I trust you will manage to do so. NSIndexPath basically tells you which part of your array should be displayed. Assuming, for now, that you are working in an ungrouped table, you would simple create a new cell and fill it with the contents of your array, which is determined by the indexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyNiceIdentifier";
cell = [aTableView dequeueReusableCellWithIdentifier:NavigationCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.tag = 500;
}
cell.textLabel = [myArray objectAtIndex:indexPath.row];
}
In the other data source method, you simple return the count for that array:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(tableView == myFirstTableView) {
return [myFirstArray count];
}
}
The method of UITableViewDelegate you will likely use most often is this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
which you use to return the desired height for a cell at a given indexPath.
Setting dataSource and delegate is as simply as doing:
myTableView.delegate = ...
myTableView.dataSource = ...
See this documentation:http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITableViewDelegate
Please also refer to this documentation:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html
I read the documentation about how to manage deletion and reordering of rows in a UITableView. I created the edit button and I'm able to delete rows. I would like the user to be able to reorder the rows as well. It seems simple, but I can't understand how to tell the cells that they can be moved.
To tell the rows they can be deleted I use the editingStyleForRowAtIndexPath, but how do I tell the cell it can also be moved and where do I set the showsReorderControl? I tried to place in cellForRowAtIndexPath, but nothing is shown.
Thanks!
You have to say that rows can be moved:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
and implement this delegate to update your data source:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
See Managing the Reordering of Rows of Table View Programming Guide for iOS
In my case I have implemented all the required UITableViewDelegate methods as mentioned in the Apple document and in the answers here, but still cannot see the reorder control. Eventually I found out it's because I overrode the layoutSubviews method without calling the super's default implementation. After I added the [super layoutSubviews], my reorder control finally appears.
The reason why we need to call [super layoutSubviews] is because when we toggle the table's editing property it would call the cell's layoutSubviews method, and the system provided controls such as the reorder control is displayed within UITableViewCell's default layoutSubviews method. Once you realize this you can also modify your layoutSubviews implementation to change the appearance of your cell depending on whether it is being edited or not to make it less clumsy when the reorder control appears.
So here is a checklist for the row reordering:
make sure the delegate methods tableView:canMoveRowAtIndexPath and tableView:moveRowAtIndexPath:toIndexPath are implemented
make sure the tableView's editing property is set to YES
If you have a custom UITableViewCell, make sure you call
[super layoutSubviews] if you override this method
Adding to #benoit answer above. If your model happens to be a mutable array, something like this would suffice for the tableView:moveRowAtIndexPath:toIndexPath:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
id objectToMove = [_objects objectAtIndex:fromIndexPath.row];
[_objects removeObjectAtIndex:fromIndexPath.row];
[_objects insertObject:objectToMove atIndex:toIndexPath.row];
[tableView reloadData];
[self saveObjects]; // A method of your own to make new positions persistent
}
try this . . .this will handle arranging and updating of cell in case of simple tableview
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
[tableData insertObject: [tableData objectAtIndex:sourceIndexPath.row] atIndex:destinationIndexPath.row];
[tableData removeObjectAtIndex:(sourceIndexPath.row + 1)];
}
I've created a custom UITableViewCell with a label and a text field.
I want to create two cells in a group to represent a nice username/password input mechanism.
I've run into some troubles figuring it out (things like the delegate/dataSource).
Is there a more direct approach to just add those two cells and get the data inserted into the text fields from code?
P.s. I want the TableView to be only at a part of my screen, the other part will be an "Enter" button...
Plus, if it can do it via the interface builder that would be great.
Although I agree with jer, some pointers about how to still use IB to make table view cells.
Add the cells to your NIB (at root level) and make them look good. Add two IBOutlet UITableViewCell *cell0, *cell1; to your header. Hook up the cells in the NIB to these outlets.
In the class which is your dataSource, do something like:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch ([indexPath row])
{
case 0: return cell0;
case 1: return cell1;
}
NSLog("Something bad happened");
return nil;
}
Interface builder doesn't know about your table view data. If you are confused about setting up a data source, then read the documentation, try the sample code, explore until things make sense. Ask when you get frustrated.