UITableView set tableview row hidden
please refer this code :-
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
if(section == theSectionWithoutARow)
{
if(shouldRemoveTheRow)
return [theArrayWithTheSectionContents count] - 1;
else
return [theArrayWithTheSectionContents count];
}
// other sections, whatever
}
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
// blah blah standard table cell creation
id theCellObject;
if(indexPath.section == theSectionWithoutARow)
{
NSInteger theActualRowToDisplay = indexPath.row;
if(shouldRemoveTheRow && indexPath.row >= theRowIndexToRemove)
{
theActualRowToDisplay = indexPath.row + 1;
}
theCellObject = [theArrayWithTheSectionContents objectAtIndex:theActualRowToDisplay];
}
// now set up the cell with theCellObject
return cell;
}
Hope this help you
In SWIFT you need to do two things,
HIDE your cell. (because reusable cell may conflict)
Set Height of cell to ZERO.
Look at here,
HIDE you cell.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let myCell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cellID",for: indexPath) as! UITableViewCell if(indexPath.row < 2){ myCell.isHidden = true }else{ myCell.isHidden = false } return myCell }
Set Height of cell to ZERO.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { var rowHeight:CGFloat = 0.0 if(indexPath.row < 2){ rowHeight = 0.0 }else{ rowHeight = 55.0 //or whatever you like } return rowHeight }
Using this you can remove reusable cell conflict issues.
You can do the same for cell?.tag also to hide specific cell by tag.
Here is my scenario. First of all, my table view is static. And in section "Account", there should alway be only one cell displayed. LoggedInCell is displayed when user is logged in, and unLoggedInCell when user is not logged in. One solution is to set their height zero, but you may encounter NSContraints error which is complex to be fixed. My solution is below:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let count = super.tableView(tableView, numberOfRowsInSection: section)
if section == 0 {
return count - 1
}
return count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
if userForApp == nil {
return super.tableView(tableView, cellForRowAt: IndexPath(row: 0, section: 0))
} else {
return super.tableView(tableView, cellForRowAt: IndexPath(row: 1, section: 0))
}
} else {
return super.tableView(tableView, cellForRowAt: indexPath)
}
}
quite simple! yes? Btw, You may have the cells height problem like me, I mean the two cells (UnLoggedInCell and LoggedInCell) have different height, we should tell the table view object that the value of cells height by doing this:
var originHeightOfUnLoggedInCell: CGFloat = 0.0
var originHeightOfLoggedInCell: CGFloat = 0.0
func recordInitialHeightOfCells() { // called in viewDidLoad()
self.originHeightOfUnLoggedInCell = self.unLoggedInCell.frame.height
self.originHeightOfLoggedInCell = self.loggedInCell.frame.height
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0 {
if userForApp == nil {
return originHeightOfUnLoggedInCell
} else {
return originHeightOfLoggedInCell
}
} else {
return super.tableView(tableView, heightForRowAt: indexPath)
}
}
Pass the cell height zero
for that specific cell in the heightForRowAtIndexPath:
, it will automatically get hidden:-
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
float heightForRow = 40;
YourCustomCell *cell =(YourCustomCell *)[tableView cellForRowAtIndexPath:indexPath];
if(cell.tag==3)
return 0;
else
return heightForRow;
}
Add the following method to your code , it will do the trick . Hope it will help you .