Пользовательский разделитель в ячейке UITablview по умолчанию получает неожиданное поведение - PullRequest
0 голосов
/ 10 сентября 2018

Пожалуйста, пройдите эти пункты

  • Привет! Я динамически добавил табличное представление в свой код.
  • Я хочу показать свой собственный разделитель в ячейке таблицы по умолчанию.
  • По умолчанию разделитель расположен на некотором расстоянии от оси Y.

Мне известен ответ, что мы можем использовать свойство inset для разделения ячеек для достижения этой цели. Но с проблемой я столкнулся с чем-то странным.

DropDownTbl=[[UITableView alloc]initWithFrame:CGRectMake(BookTypeTxt.frame.origin.x, BookTypeTxt.frame.origin.y+BookTypeTxt.frame.size.height, BookTypeTxt.frame.size.width-11, height)];
DropDownTbl.separatorStyle = UITableViewCellSeparatorStyleNone;
DropDownTbl.tag=98;
DropDownTbl.delegate=self;
DropDownTbl.dataSource=self;

[self.settingView addSubview:DropDownTbl];
[DropDownTbl reloadData];

И в моей ячейке табличного представления я добавил разделитель в этом стиле, проблема в том, что представление разделителя добавляется дважды, как показано на данном изображении

UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if(cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

cell.textLabel.text=[BookTypeArray objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:15.0f];
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,cell.contentView.frame.size.height-1, DropDownTbl.frame.size.width, 1)];
lineView.backgroundColor = [UIColor blackColor];
[cell.contentView addSubview:lineView];

cell.textLabel.font=[UIFont fontWithName:@"Arial" size:12];

return cell;

enter image description here

Поэтому я решил добавить linview в ячейку == nil круглых скобок

UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,cell.contentView.frame.size.height-1, DropDownTbl.frame.size.width, 1)];
    lineView.backgroundColor = [UIColor blackColor];
    [cell.contentView addSubview:lineView];
}

cell.textLabel.text=[BookTypeArray objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:15.0f];
cell.textLabel.font=[UIFont fontWithName:@"Arial" size:12];

return cell;

Таким образом, мой сепаратор исчезает при повторном использовании клетки. Я просто хочу знать, почему это происходит?

1 Ответ

0 голосов
/ 10 сентября 2018

Эта проблема возникает из-за того, что cellForRowAt вызывается несколько раз, когда вы прокручиваете свой TableView каждый раз, когда этот код добавляет новую строку в ячейку contentView.

Попробуйте это.

[[cell.contentView viewWithTag:5] removeFromSuperview];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

}

UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,cell.contentView.frame.size.height-1, tableView.frame.size.width, 1)];
lineView.tag = 5;
lineView.backgroundColor = [UIColor blackColor];
[cell.contentView addSubview:lineView];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...