Customize height of n cells in UITableView, but not all the same - iphone

thanks to the folks here I already learned quite a lot on my way to a cool iPhone App I am working on. However, I was wondering if anyone found out how to manipulate a UITableView, so that a cell (any or, if that is not possible, it could only be the selected one) can have a different height.
I know I can use something like this:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70;
}*/
To modify the whole TableView. But how would I address this to a single, specific cell?
The final goal is to achieve a "OS X dock"-like zoom effect when scrolling through a table...
Any help is appreciated.
Best regards,
J*

The method that you're citing there in your question is exactly the method you want to use. The code you posted always returns a fixed value. But the indexPath parameter is there so that you can use that in whatever conditional processing you might want to do. For example, determine if that row is selected, and return a different height.
You'll also want to take care that the cell you return from -[UITableView cellForRowAtIndexPath:] matches this height.

You use the indexPath row and section to determine and return the height for the cell.
All of the methods related to UITableViewController give you an indexPath that will correspond to the cell when asking for specific information. For example, when asking for the actual cell to return:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
What to do when a cell was selected:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Height of the cell:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
And several others. Check out a UITableViewController tutorial to get the hang of how it works as a delegate and a dataSource for a UITableView.
Here is a good one: http://adeem.me/blog/2009/05/19/iphone-programming-tutorial-part-1-uitableview-using-nsarray/

Related

UITableView not able to fetch data from array

i am making iPad application, in which i am fetching data from Url, after fetching data from URL,
i am storing into array,
when i write NSLOG inside this two TableView method,
it works properly,
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLOG(#"ARRAY=%#",arrayname);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLOG(#"ARRAY=%#",arrayname);
}
but when i write this NSLOG inside this method thn it shows error,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLOG(#"ARRAY=%#",arrayname);
}
i also declared nsmutablearray properly....
it shows EXC_BAD_ACCESS
why this any idea ?
The method cellForRowAtIndexPath must return the cell, otherwise you'll see the error you see. But if the code above is 'metacode', and you put you NSLog somwhere between the proper strings of code, then you can follow Alex Reynolds' advice.
Check if your array is still alive. E.g. you can set a breakpoint and check if the program falls because of turning to 'arrayname', and if so, try to find where it could be released in your code by this moment. Also, perhaps you're just create your array with wrongly.

What should cellForRowAtIndexPath return if the table is empty?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I think if the table is empty, namely that
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[BNUtilitiesQuick getBizs] count];
}
always return 0
I would expect that -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath should not be called at all
This is important. I am making a program to search through things and sometimes after some searching, there is no result being returned.
However, is called anyway and I got an exception. What should I do?
-tableview:cellForRowAtIndexPath: may be called before the table view has realised it has zero rows and so shouldn't be called.
Therefore your implementation needs to check that the value of the row being passed in is not outside the bounds of your array. If it is, you need to return an empty cell (the documentation states returning nil will raise an exception).
There is simply no way that tableView:cellForRowAtIndexPath: will be called when the tableView:numberOfRowsInSection: method returns 0. It is likely that the count is being returned incorrectly.
Often you would have a giant switch statement in a cellForRow... Meaning that you have to have a value to return in the default case. I usually return nil there.
This is required by the compiler to not show a warning, but it should never be actually called. This method is only called for row indexes that are possible due to what you reeturn in the numberOfSections and numberOdRowsInSection methods.
First print [[BNUtilitiesQuick getBizs] count];
in NSLog and see if it really returns 0.
If it shows 0, and if the cellForRowAtIndexPath: still gets called, I suspect there is some ghost hanging around there. :-)
What about returning 1 as count (or 10 for example), and that one cell will display dimmed text with "No Results" ?
(open your contacts app and search for something.. it will have about 10 rows , 9 are empty and 1 displays "no results")

NSMutableArray to table view

i am new to the iPhone development.
i need to display the NSMutableArray contents into UITableViewCell..
it is quite simple.. but, i want to know, how to add the NSMutableArray contents into table view at runtime?
please anyone help me..
thank you very much..!
Have you read the Table View Programming Guide?
In particular the section that talks about Creating and Configuring a Table View.
The little code snippets on those guides provide examples that deal with the simplest case, which is an array.
In short, you provide a "data source", which the table view asks for each row from. That data source is usually (but doesn't have to be) the ViewController that uses the table.
Make sure to check out the example applications linked to there if you need to see some working examples in Xcode itself.
You want to add total array to single cell or a tableView. If the answer is tableView, then you can directly give the values in the following method like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell.textLabel.text = [array objectAtIndex: indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array count];
}
you can use like that.

How to use UITextView in UITableViewCell with variable height?

I am interested in creating a UITableViewCell with a UITextView inside of it, which adjusts its height (to a certain maximum) as the user types more or less text into the UITextView. I know how to add the UITextView to a custom UITableViewCell--but how might I change the UITableViewCell's height as the user is in the process of typing? Is this possible to do?
As far as I know the only way to set the height of a table cell is through the delegate heightForRowAtIndexPath: method. To invoke this method you can call reloadData on the table but I'm not sure what effect that would have on the UITextView you are editing. Worth a try though.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
To calculate the return value you would need to work out the height based on the current input and expand the text view at the same time. NSString has some helpful methods for this such as sizeWithFont:constrainedToSize:lineBreakMode:. It will work out a CGSize you can use to make your CGRect etc.

How can I find out the name, ID, or type of a UITableView Cell?

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if([tableView.somethingMagicalHereThatAllowsMeKnowWhichCellItIs isEqualToString:#"CellType"]){
return 50;
}
return 25;}
I have tableView with multiple cells of different types, and I want to style them accordingly, but the problem is that I don't know which type it is when it comes in. I can't use indexPath because the cells are in no specific order. Is there a way to show the id?
What I usually do is remove the “what goes in which table view cell” from -tableView:cellForRowAtIndexPath: and put it in a separate method. How I name this depends on what I’m trying to achieve. One typical scenario is that each cell represents an object. In that case, I define a method like this:
- (SomeObject*)objectForRowAtIndexPath:(NSIndexPath*)indexPath;
Then, in both -tableView:cellForRowAtIndexPath: and -tableView:heightForRowAtIndexPath:, I call that method to find out which object corresponds to that cell, then either create the cell or set its height accordingly.