I am working on a custom tableviewcell and trying to increase its height. I dont want to do it from my viewcontroller. Can someone please help me?
Thanks
You actually need to do it from your UITableViewDelegate.
What you can do though, is use UITableView's cellForRowAtIndexPath method to get the actual cell and call isKindOfClass: on it, and if it matches the type of your custom cell, you're golden.
Implement the tableView:heightForRowAtIndexPath: method and return the desired height for the specified row:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if([cell isKindOfClass:[YourCustomCell class]]) return someHeight;
return 44.0;
}
I'm afraid you have to do it from your controller in the tableView:heightForRowAtIndexPath:. There is no other way to do it. :(
Related
I have created two types of prototype cells in storyboard. The dimension of one of them have been customized to accomodate UIButton object. However when the cells are created, they have the standard height. I can see the UIButton object but it gets truncated because of the cell height.
Why are the newly created cells different from the prototype cells?
The relevant section of the code is as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if(cell == nil)
{
cell = [tableView dequeueReusableCellWithIdentifier:#"PictureSelectionCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
pictureButtonProperty = (UIButton *) [cell viewWithTag:1];
}
}
Going forward, what are my options for creating the cell of the width (or dimensions) defined in the storyboard? Programmatically, I will be able to achieve this by creating a CGRect object with the specified dimensions and then create a cell using initWithFrame. However, I would like to avoid doing things manually.
Thanks for your response.
first of all you can always set it with code
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return YOUR_ROW_HEIGHT;
}
other way if you choose your UITableView if the storyboard, under the size inspector change the Row Height.
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
//...
I am new to the iPhone development. I got stuck with a problem. I want a check box function implemented in my UITableView. But my UITableViewCells are custom cell which consist of image and 3 UILabels. I could bring a sub view to the table view so that check box image can placed and I am successful in that. But the problem comes is whenever I click on the image only the last cell check box get changed. I would like to access the cell which I've clicked.
I have tried this
UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]
But this crashes since cell is custom.
Can any one suggest me a good method to do that?
Instead of using
UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]
try using
YourCustomCell *cell = (YourCustomCell *)[self.tableView cellForRowAtIndexPath:indexPath];
Hope it helps
First of all you need to define a tag -
#pragma imageViewTag 1
then in your cellForRowAtIndexPath assign this tag to your image view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
then you can access your image view any where with the help of this tag as -
UITableViewCell *cellView = (UITableViewCell*) [tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
UIImageView *imgView = (UIImageView*) [cellView viewWithTag:imageViewTag];
In your TableView DidSelect Delegate method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIImageView *image = [cell.contentView viewWithTag:15];
//You should have set the tag value as 15 when creating the image in cell
//and you should have added to the cell contentview
}
If you cant able to get the cell then probably you wouldnt have used the reusable cell concept correctly. So post your entire cellforrowindex code.
If you are using the check kind of thing. Why dont you use the default disclosure indicator of tableview instead of your image.
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.
I have a UITableView, and in the tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath, I want to be able to access the frame that the selected cell is in. Here is what I was trying:
UIView *cell = tableView.indexPathForSelectedRow;
UITableViewCell *cell = (UITableViewCell *)indexPath;
I want to be able to access the view that the selected cell is in.
thanks in advance.
To do this, use:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
than you can access all the info on cell as you would any other UIView
ie. cell.frame, cell.bound, etc.
Accessing the cell directly isn’t necessary—yes, you can get the cell and its frame via -cellForRowAtIndexPath:, but you’d then have to subtract the content offset of the UIScrollView (which UITableView subclasses from) to get the cell’s actual position within the boundaries of the table view. The best approach for this is the UITableView method -rectForRowAtIndexPath:.