Is it possible to hide/remove the first tableviewHeader only?
basically I want to show a custom cell which will be designed as an offer - I dont want this to have a header - could I add this logic to my heightForHeaderInSection method -
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 40;
}
Check with this:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if(section == 0)
{
return 0;
}
return 40;
}
Or you can implement viewForHeaderInSection
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(section == 0)
{
return nil
}
//else return header view
}
Yes you should do:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0)
return 0.0;
else
return 40;
}
And also if you use titleForHeaderInSection: you should return nil when section = 0.
Try this:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if(section == 0)
{
return 0;
}
return 40;
}
Try the following way for your table view's headers
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0)
return 1.0f;
return 40.0f;
}
- (NSString*) tableView:(UITableView *) tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return nil;
} else {
// return some string here ...
}
}
- (void) viewDidLoad
{
[super viewDidLoad];
self.tableView.contentInset = UIEdgeInsetsMake(-1.0f, 0.0f, 0.0f, 0.0);
}
Related
I have two UITableViews in the same ViewController, one which uses dynamic height based on UILabel content size and the other where all the cells are static heights. I separated the two TableViews using tags. I tried using this:
self.tableView1.rowHeight = UITableViewAutomaticDimension;
and:
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView.tag == 1) {
return 114.0f;
} else {
if (indexPath.section == 0) {
return 50.0f;
} else if (indexPath.section == 2) {
return 20.0f;
} else {
return 44.0f;
}
}
}
The problem is that the dynamic cells do not change dynamically and the static height cells originally have the right height but when they scroll out of the view they then change to the default height for cells.
Can anyone help please?
When you implement:
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
You need implement both this and
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Normally It´s the same code, add this to your protect:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView.tag == 1) {
return 114.0f;
} else {
if (indexPath.section == 0) {
return 50.0f;
} else if (indexPath.section == 2) {
return 20.0f;
} else {
return 44.0f;
}
}
}
I have a tableView with static cells. If one of the cells are out of the view, the cells disappear and everything overlaps. Here's a screenshot:
This is the relevant table view code:
- (BOOL) tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 1;
} else if (section == 1) {
return 4;
} else if (section == 2) {
return 3;
}
return 0;
}
Thanks
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1) {
return NO;
}
else {
return YES;
}
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1) {
return NO;
}
else {
return YES;
}
}
I have 2 sections in my tableView. I want my tableViewcells to be moved only in first section(not for second). Currently if i drag my tableCell to second section, It has a crash.
I think you are looking for this method -
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if (proposedDestinationIndexPath.section != 0)
{
return sourceIndexPath;
}
return proposedDestinationIndexPath;
}
It will restrict you to move your rows in section 0 only. If you try to move row from section 0 to 1, it'll return the previous indexpath of the cell
I've looked around at other questions here and many other sources online and just can't find a solution. I have 3 rows and no matter what I do, they all keep the width defined in the tableview. The first one is what I want, but the second and 3rd I can't give a custom height.
Below is my code:
#import "DetailTableViewController.h"
#import "EventDetailTableViewCell.h"
#import "EventDescriptionTableViewCell.h"
#import "EventMapTableViewCell.h"
#interface DetailTableViewController ()
#end
#implementation DetailTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
static NSString *CellIdentifier = #"headerCell";
EventDetailTableViewCell *headerCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
headerCell.headerImage.image = [UIImage imageNamed:#"img4.png"];
return headerCell;
} else if (indexPath.section == 1) {
static NSString *CellIdentifier = #"descriptionCell";
EventDescriptionTableViewCell *descriptionCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
descriptionCell.descriptionImage.image = [UIImage imageNamed:#"img2.png"];
return descriptionCell;
} else {
static NSString *CellIdentifier = #"mapCell";
EventMapTableViewCell *mapCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return mapCell;
}
}
#pragma mark - Helper Methods
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 1) {
return #" ";
} else if (section == 2) {
return #" ";
} else {
return nil;
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return 222;
} else if (indexPath.row == 1) {
return 80;
} else {
return 100;
}
}
#end
I believe there is a mistake in your code in heightForRowAtIndexPath. You have been using indexPath.sectionfor most of your TableView delegate and dataSource methods. So, I believe you want to use indexPath.section on heightForRowAtIndexPath as well.
The following code should work.
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1) {
return 222;
} else if (indexPath.section == 2) {
return 80;
} else {
return 100;
}
}
That is:
if ([self.tableView numberOfRowsInSection:0] == 0) {
self.tableView.tableFooterView.hidden = NO;
} else {
self.tableView.tableFooterView.hidden = YES;
}
But I hope it can update automatically.
You can implement this function :
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if ([tableView numberOfRowsInSection:section] == 0) return ***height***;
else return 0;
}
Finally I put the show/hide tableFooterView logic into:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
It works well for me.