static cells in tableview disappear when out of view - ios

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

Related

Some resizing cells and some static in two UITableViews

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;
}
}
}

Ristrict UITableViewCell to move in particular section

- (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

UITableViewCell - No Section Header only for first cell

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);
}

Automatically show tableFooterView if numberOfRowsInSection is zero?

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.

iOS - how to increase height of first row of UITableView only if Table is empty?

I am new at iOS development.
I want to check if my table is empty. If it is, I want to :
Increase height of the first row and display "No new messages!"
OR
Get rid of the table and just display "No new messages" in the center of the page.
How would I do that?
(Let's assume you have an array from which you want to populate the table view. This is a pretty standard way for populating table views. Let's call this theoretical array dataArr.)
In your data source:
- (NSUInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSUInteger)section
{
if ([dataArr count] == 0) {
// empty table
return 1;
}
return [dataArr count];
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)ip
{
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:#"someCellID"];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"someCellID"] autorelease];
if ([dataArr count] == 0) {
// empty table
cell.textLabel.text = #"No new messages";
} else {
// configure as normally
}
return cell;
}
In your delegate:
- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)ip
{
if ([dataArr count] == 0) {
// empty table
return 88.0f; // 2 times the normal height
}
// else return the normal height:
return 44.0f;
}
I suppose you have an array of messages you are attempting to display
You can define custom height for cells with the function
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (messages.count == 0)
return 80;
else
return 44;
}
and then, when you are creating the cells: (make sure the "no new messages" cell has a different cell identifier than the regular cells, so that your cell re-use won't mess things up)
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (messages.count == 0) {
// create the "No New Messages" cell
}
else {
// create regular cells
}
}
This should help:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView numberOfRowsInSection:1] == 0) {
return 200;//some other height
}else{
return 44;
}
}
Basically you want to check against your data source to see if you have no messages in the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath and - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
For example:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([data count] == 0) return 100;
else return 44;
}

Resources