Adjust UITableViewCell Height and Indentation? - iphone

Two UITableViewCell related questions:
In my custom UITableViewCell I loop through an array (of which I do not know how many objects it holds) and add a UILabel displaying some text for each object in that array.
This means I have to adjust the height of the cell so that these labels fit in. How can I do this?
When going into edit mode, I have the cells indent, however I do not want this. I have tried the following:
cell.shouldIndentWhileEditing = NO;
and
-(BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
Both sadly failed, I have I no idea why. How could I possible remedy this?
Any help is much appreciated with either of these issues, thanks.

You can specify the height for every row with the delegate method [UITableViewDelegate tableView:heightForRowAtIndexPath:].
Just change what the method returns and the reload your table.
This method actually has nothing to do with the indendation of cell content:
Asks the delegate whether the background of the specified row should be indented while the table view is in editing mode.
You can try to set indentationWidth but I never managed to make it work.
Fortunately, it's easy to change everything you want in [UITableView layoutSubviews] method.
Example:
- (void)layoutSubviews {
[super layoutSubviews];
self.contentView.frame = self.bounds;
}

You may also need to set
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
{
return UITableViewCellEditingStyleNone;
}
For the dynamic height part there are plenty of other answers and a quick google found a tutorial here

Related

Collapsing and Hiding static UITableViewCells of a UITableview

I have been though a ton of examples that show how to do a collapsable tableview but I cant find something that works with static cells. I have a couple cells that I made in IB that I would like to collapse if the section header is clicked but I cant figure out or understand how to do it. If anyone can tell me or point me to an example that does that, I would be grateful.
Here is an example of just one of the cells I have that would need to collapse:
You can refer to this sample: (download the sample code)
http://developer.apple.com/library/ios/#samplecode/TableViewUpdates/Introduction/Intro.html
So basically what this code does is:
You create Section in your -(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section --> Each Section has SectionHeaderView --> Which you basically will tap and expand your table view.
You will implement the two delegate methods:
-(void)sectionHeaderView:(APLSectionHeaderView*)sectionHeaderView sectionOpened:(NSInteger)sectionOpened
-(void)sectionHeaderView:(APLSectionHeaderView*)sectionHeaderView sectionClosed:(NSInteger)sectionClosed
--> This will internally call insertRowsAtIndexPaths and deleteRowsAtIndexPaths --> Which will internally call tableview:cellForRowAtIndexPath --> Now here you need to create your cell which will have address, city etc etc...
Let me know if you need more information.
I finally found a solution I was happy with thanks to this: https://github.com/xelvenone/StaticDataTableViewController
A very simple solution with the implementation:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.reloadTableViewRowAnimation = UITableViewRowAnimationMiddle;
self.deleteTableViewRowAnimation = UITableViewRowAnimationMiddle;
self.insertTableViewRowAnimation = UITableViewRowAnimationMiddle;
if (indexPath.row == 0) {
[self toggleCellVissibilityOrReload:self.cell1];
}
[self reloadDataAnimated:YES];
}

UITableViewCell Animation

I am stuck with a tableview where cell are displayed in stack format.Any suggestion from expertise how to implement this kind of tableview.
(i.e i need to display the one cell behind another cell. On Tapping any cell, the cell should animate and shows complete contents of cell)
EDITED:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
int defaultCellHeight = 30;
if(isSelected)
defaultCellHeight = 60;
return defaultCellHeight;
}
You can create this type of TableView by giving shadow of UITableViewCell.
Here i give you to Some links that might be helpful in your case.
https://github.com/mystcolor/jtgesturebasedtableviewdemo
https://github.com/OliverLetterer/UIExpandableTableView
https://github.com/binho/TicketSaldo
Follow This is best the Site for all Controller Ever.
https://github.com/bobmccune/Core-Animation-Demos If you look for core animation,then hope this demo works for you.

how to remove button in Uitableview when table is int edit mode

I creating the one UITable view with the 2 sections in with when i load the data into the uitablew it's working fine,
But my problem is how to resolve one sorting button is disable in section 0 and row 0
which more discribe into the following image.
please help to solve my problem
in advance thanking to your valuable time spend on my problem.
It's quite easy.
Listen on the UITableViewDataSource call tableView:canMoveRowAtIndexPath:
Like this:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0) {
return NO;
}
return YES;
}
If you don't not where to place the method, place it next to tableView:cellForRowAtIndexPath:
Set showsReorderControl to NO property of cell
cell.showsReorderControl = NO;
In addition, if the data source implements tableView:canMoveRowAtIndexPath: to return NO, the reordering control does not appear in that designated row. You need to check indexPath and return NO for it.

UITableView sections text alignment

can someone help me with text alignment to the right in my uitableview sections ?
the defult alignment is left.
if the only way is to build custom header i will appreciate for code example.
try with this
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//create a uitextfield and set font you want in
return uitextfield ;
}
it seems working for me.
Have you tried adding
cell.textLabel.textAlignment = UITextAlignRight;
to tableView:cellForRowAtIndexPath:?
As long as you are using cell.textLabel.text, and not the deprecated cell.text to set the text of the cell, this should work.
edit:
Sorry, I didn't catch that you were trying to do this with the section headers. I think you're going to have to roll your own header views, and return them using the UITableView delegate method -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
Could take some work to get them to look like the default ones. Here is an example of someone describing how to theme the section headers: http://iphoneinaction.manning.com/iphone_in_action/2009/07/beauti.html
try this it is suitable for right to left languages
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
UILabel *lab = [[self.tableData objectAtIndex: section] valueForKey:kSectionTitleKey];
lab.Text = #"right to left alignment text";
lab.textAlignment = UITextAlignmentRight;
return lab.text;
}
as you see in code this is the only way to align text from right to left, create a custom label, set its text property and alight the label to the right.

iPhone + UITableView + row height

I am setting the row height of my UITableView using following code
[tableView setRowHeight: 100.00];
I am using the single line as separator in the UITableView.
Eventhough setting the height above, height of row does not get change.
You should implement
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
delegate method. and return 100.0 there.
You should avoid the heightForRowAtIndexPath if all your rows are of similar height and use the rowHeight property. According to the documentation:
There are performance implications to using tableView:heightForRowAtIndexPath: instead of rowHeight. Every time a table view is displayed, it calls tableView:heightForRowAtIndexPath: on the delegate for each of its rows, which can result in a significant performance problem with table views having a large number of rows (approximately 1000 or more).
In the UITableViewController subclass it could be done, for instance, in the viewDidAppear method (the UITableViewController has a reference to the tableView):
self.tableView.rowHeight = 79.f;
The row height is baked into the cells when they are first displayed.
Did you set UITableView#rowHeight before setting the data source?
If not, do so.
If for whatever reason you can't, your other option is to call UITableView#reloadData after setting the row height.
I did like this, here tableobj is nothing but UITableView object in my application.
- (void)viewDidLoad
{
[super viewDidLoad];
[tableObj setRowHeight:100.0f];
}
Or handle it in numberOfRowsInSection: like:
- (NSInteger)tableView:(UITableView *)tblView numberOfRowsInSection:(NSInteger)section {
[tableObj setRowHeight:100.0f];
return [soandso count]; // soandso is my object
}
Because set the rowHeight before setting the data source. It worked for me (for equal row heights).
The better and cleaner solution is to implement the delegate function (Maybe not the best one if your UITableView has many rows ...).
Also, think that UITableVieCell are UIView so you could change their height firstly...
Delegates are very powerful in iPhone dev, here the UITableViewDelage:
http://developer.apple.com/iphone/library/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html
You can also change the indentForRow, displayCellForRow, heightForRow,edit stuff .....
I dont think there is such a method in UITableView...
Instead you can use the property rowHeight...
Try,
tableView.rowHeight =100;