How to manually set UITableViewCell? - uitableview

I created UITableVewCell subclass for my cell and I want set height of cell to 70px. It's possible? How can I do that?

I found solution;
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70;
}

You need to use the Delegate method of UITableView,similarly there are lot's of method for other purpose too.
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70;
}
For more details follow the below link:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40006942-CH3-DontLinkElementID_3

Related

How to make the height of custom UITableViewCell dynamic for each cell made in the UITableView?

I have to make the height of UITableViewCell dynamic with respect to number of images in the cell.
Add following lines of code in your viewDidLoad():
tableView.estimatedRowHeight = 200.0
tableView.rowHeight = UITableViewAutomaticDimension
where estimatedRowHeight is the maximum height you want to set for UITableViewCell.
Also, add this delegate method and return UITableViewAutomaticDimension:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
set the UITableView's rowHight property to UITableViewAutomaticDimension and set the estimated row height to what the most common height is. Then in your layout of the cell make sure there are constraints between all of the visual elements vertically and the cell will be laid out properly. The vertical constraints should be something like:
V:|-(5)-[imageOne]-[imageTwo]-|
Where "|" is the cell's content view.
Implement the delegate method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath (NSIndexPath *)indexPath
#interface yourController : UIViewController<UITableViewDelegate,UITableViewDataSource>
set delegate in view did load
- (void)viewDidLoad {
[super viewDidLoad];
tableview.delegate=self;
tableview.dataSource=self;
}
now method of height of table view cell
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;//your value
}
in cell
- (CGFloat)cellHeight {
[self layoutIfNeeded];
return CGRectGetMaxY(_imageview.frame) + 10;
}
the imageView is the last view in cell. It only needs maxY of the lastView
in controller
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
OJTopicRootCell *cell = (OJTopicRootCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.cellHeight;
}
Use the estimatedHeight methods to quickly calcuate guessed values which will allow for fast load times of the table.
If these methods are implemented, the above -tableView:heightForXXX calls will be deferred until views are ready to be displayed, so more expensive logic can be placed there.
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0);
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);

How to dynamic cell height with Parse?

Here is my storyboard:
The method i use to get the image is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
cell.mainImage.file = (PFFile *)object[#"image"];
[cell.mainImage loadInBackground];
}
Row Height is set as:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 501.0f;
}
I also would like to have the Image to be filled perfectly, much like instagram. I dont want images to have bars on the top and bottom.
this was work for me
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}

How to hide the headers of my UITableViews sections programmatically?

I want the headers and footers for my UITableView's sections to be hidden, and I'm currently setting them up like so:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 0.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return nil;
}
As you can see in the screenshot, this has successfully hidden the footers, but not the headers (shown in gray). What's missing?
Method heightForHeaderInSection doesn't work with 0. Try this one:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0.0001f;
}

Remove text of uitableviewcell when you are delegating two uitableview

I have a uitableviewdelegate class that is delegate of two uitableview. I want that one of them has remove button and the other do not have remove button. I used below code:
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView==self.firstTableView){
return #"Remove";
}else
return #"";
}
button my code do not disable editing style for secondTableView it just set empty text as title. How can I do this?
EDIT
I also use below methods:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == slef.FirstTableView){
//Some codes
}
}
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == self.FirstTableView){
//Some codes
}
}
but any of them was not helpful.
Please implement this delegate method.
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView==self.firstTableView) {
return NO;
}
return YES;
}
Use delegate method
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
Here you can return UITableViewCellEditingStyleNone or UITableViewCellEditingStyleDelete for desired table

How to hide a static UITableViewCell?

How do you hide a static cell?
I would like to hide and static cell if an image does not exist.
I tried:
imageCell.hidden = YES; //did not work
I have seen answers suggesting to change datasource or use:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 0;// will hide all cell
}
But I couldnt find a way to do this with a specific view cell.
what I want to achieve:
if(image==nil){
//hide imageCell
}
Now here is the catch , the image is downloaded asynchronously, so deleguate methods might be called before the attempted downlaod.
Do the following :
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 2 && !myImageIsLoaded)
return 0; // Will hide just the third row of your table if myImageIsLoaded is false
return 44;
}
And you can use the following to animate all whenever you want (e.g. each time an image as loaded) :
[myTable beginUpdate];
[myTable endUpdate];
If your cells are static so it should work. Otherwise you could encounter some problems.
If you want literally hide the cell that image does not exist, you can try this:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
return [cell imageView] ? 44.0f : 0.0f;
}

Resources