UITableView set tableview row hidden
У меня есть пользовательская ячейка tableview в grouptableview. И у меня есть один скрытый. Затем я должен сделать это видимым. Ячейка ячейки - 3.
Это не работает мой код:
if (self.tableView.tag == 3) {
self.tableView.hidden = NO; //Not working.
}
Просто мне нужно сделать одну строку видимой. Надеюсь, вы понимаете.
Ответы
Ответ 1
Пропустите высоту ячейки zero
для этой конкретной ячейки в heightForRowAtIndexPath:
, она автоматически будет скрыта: -
- (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;
}
Добавьте следующий код в свой код, он будет делать трюк.
Надеюсь, это поможет вам.
Ответ 2
В SWIFT вам нужно сделать две вещи:
Посмотрите здесь,
-
Скрыть вашу ячейку.
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
}
-
Установите высоту ячейки в 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
}
С помощью этого можно устранить проблемы с конфликтами со многократным использованием.
Вы можете сделать то же самое для cell?.tag, чтобы скрыть конкретную ячейку по тегу.
Ответ 3
см. этот код: -
- (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;
}
Надеюсь, что эта помощь вам поможет.
Ответ 4
![enter image description here]()
Вот мой сценарий. Прежде всего, мой табличный вид статичен. А в разделе "Аккаунт" всегда должна отображаться только одна ячейка. LoggedInCell отображается, когда пользователь вошел в систему, и unLoggedInCell, когда пользователь не вошел в систему. Одним из решений является установка нулевой высоты, но вы можете столкнуться с ошибкой NSContraints, которую сложно исправить. Мое решение ниже:
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)
}
}
достаточно просто! да? Кстати, у вас может быть проблема с высотой ячеек, как у меня, я имею в виду, что две ячейки (UnLoggedInCell и LoggedInCell) имеют разную высоту, мы должны сообщить объекту табличного представления, что значение высоты ячеек выполняется следующим образом:
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)
}
}