Setting Line Height on UITableViewCell's detailText - iphone

Here I'm using the default UITableViewCell & the thing I don't like is that the detailedText is too close to the textLabel so I wanted to add some spacing between them, but I cannot find how to do that. Any suggestsions?

Override this below delegate method.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

Related

Simplest way for UISearchDisplayController to show same cells as the origin

What's the simplest way to make UISearchDisplayController display exactly the same cells (formatting, height, fonts, colors, etc) as the original tableView that it searched on?
or simply put - make it dequeue de same cell identifier?
I am using a the standard subclassed UITableViewController + UISearchDisplayController solution, and prefer to stick to it.
thanks
I actually found the simplest way.
All I had to do is change the default cellForRowAtIndexPath second line of code and add the "self." to tableView to the dequeue cell identifier - this way the the cell style which is in the storyboard always gets dequeued and not the (non-existant) one from the searchResultsController.tableView
Also implement heightForRowAtIndexPath that will return the same height without any check (searchresults table or self.table - we want the same height)
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 62.0f;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"PhraseCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// ^^^^ NEW
//...

update title of selected row in Uitableview

i'm using the following code to get the selected cell in the UiTableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// Mofify the cell here
}
when i change the cell text or color it does not make any change in the actual UitableView
I would suggest that you reload the cell. Use:
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:NO];
You should keep track of which cell was selected, and update the cell in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I don't know why the text isn't updating for you when you change it in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
but even if you get it to work, it will revert if you scroll that cell off the screen.
UITableView is a subclass of UIView and UIView has this method to force it's refresh
- (void)setNeedsDisplay
But if you don't take that change into account in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
you may never see the change.
UITableViewCell has the -titleLabel method which returns a UILabel. Then you might use the setText: and -setColor: methods.
Try doing:
Set the backgroundColor property of the UITableViewCell.
Set the backgroundColor property of the UILabel (may not do what you need).
Set the backgroundView property if the UITableViewCell to a custom class you make by subclassing UIView, where you create a label and color.
Also, you can't just change the cell color, you need to have a data source from which cellForRowAtIndexPath: reads, which sets the cell color there. This way, the color change will persist.

Padding on TableViewCell

I want to add padding to my table view cells. Thought I could do something like this but that didnt work.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
return cell.frame.size.height + 20;
}
See the below link. Explains the basics of getting a variable height (for cells) UITableView along with proper padding that your require. Example just includes text. Change it to suit your requirements within the cell.
http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/
Are you sure you placed this method in your delegate? it also depends on which cells. There are other methods...
// UITableViewDelegate Method
- (CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
}
// UITableViewDelegate Method
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
}
also, if you are using the contentView of the cell then you will need to change the frame of your custom view
When adding your cells you will need to set the y value of the cell's frame to half your padding to make it start at the right place. Also ensure that the cell does not have flexible height or it will expand to fill the height of the row.

UITableView cell height setting life-cycle

Trying to understand when the cell height is set.
I have a UITableView that is created in layout, not code.
Then:
#define TABLE_VIEW_ROW_HEIGHT 100.0
Followed by:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Setting row height");
return TABLE_VIEW_ROW_HEIGHT;
}
and
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Row height: %f", tableView.rowHeight);
// ... other happy stuff that does not change row height but could sure use the real row height
The last NSLog reports 44 rather than 100. Which means that the code within that method that needs to get the row height is not using the real height.
I've verified that
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
runs before
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
And, as a result, the rows are of the desired size, not the default size of 44.
Do I need to explicitly set the height somewhere else?
EDIT AFTER DOING SOME RESEARCH:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
...is more appropriately used to set the height of rows when not all rows are of the same height. I was wrongly using this to set all rows to the same height.
The right way to do it is to call
the_tableview.rowHeight = TABLE_VIEW_ROW_HEIGHT;
...from somewhere like viewDidLoad. I actually did that (code omitted) but it didn't work. Tonight I realized that this is yet another instance of being had by the graphical layout tool...I forgot to connect the IBOutlet for the UITableView...so, my code did nothing and the entire tableview remained at the default height of 44. That's why I was getting 44 out of my NSLog.
In your code, you are calling the rowHeight on tableView and not on a specific row at IndexPath. That is the reason why you are getting the default value of 44. If you want to change the default value then you can do it in size inspector for the table. Changing the default value is better if all the rows have the same height. If there are rows with different heights then you will have to implement heightForRowAtIndexPath for that the rows with different heights.
You have defined TABLE_VIEW_ROW_HEIGHT in code then in this case you will also have to set the tableView.rowHeight in code by assigning it the defined TABLE_VIEW_ROW_HEIGHT name.

tableView cell height... how to customize it?

Is it possible to modify height of only one cell in a grouped table view?
I have a table view with 2 sections of 3 and 2 rows... I would change row height of the second row of the second section...
How can I do this?
Thanks!
You can look at this method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
In your case, the code should look like:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1 && indexPath.row == 1) {
return SPECIAL_HEIGHT;
}
return NORMAL_HEIGHT;
}
You can look for more details about the method here
In iOS 8 and above we can use the Dynamic Table View Cell Height.
Using this feature UITableviewCell get its height from its content and We don't need to write heightForRowAtIndexPath
All I have to do in viewDidLoad()
tableView.estimatedRowHeight = 44.0;
tableView.rowHeight = UITableViewAutomaticDimension;
If cell contains uilabel. then apply constraints to uilabel
Make sure you change Label --Lines-- to 0
The cell will grow automatically with content of uilabel:
You can implement the following method to return the height for the row at a given index path:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html%23//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath:
Have a look at
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
For the indexpath just check for which row and section and adjust the height accordingly