Когда я устанавливаю rowHeight для UITableView, разве не должна изменяться и высота ячейки? - PullRequest
4 голосов
/ 13 декабря 2011

Вопрос:
Когда я устанавливаю rowHeight для UITableView, разве не должна изменяться и высота ячейки?
Вот ситуация, которая заставляет меня задуматься об этом:


Я хочу установить отдельную строку для каждой ячейки табличного представления внизу, более того, я хочу установить для нее высоту строки от 44 до 32. Результат, который я хочу, выглядит следующим образом:
image

Row height setting is done correctly:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.tableView setRowHeight:32.0f];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

    //...
}

// Even use the delegate of UITableView
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 32.0f;
}

However, when I was adding separate line for cell, I met a issue: I want to set the separate line at bottom of the cell, so I set the y position by cell.frame.size.height - 1.0f. Unfortunately, the result shown as below:
image

When I did selecting, cell changed like below:

1. Selected row No.1:

image

2. Selected row No.2:

image

3. Selected row No.3:

image

It seems that the height of cell before selected was 44, when selected, it changed to 32. They were overlapped one by one like cards, right? Weird!

The main code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
        [cell.textLabel setFont:[UIFont fontWithName:@"Futura-Medium" size:15.0f]];
        UIView * seperateLine = [[UIView alloc] initWithFrame:CGRectMake(10.0f, cell.frame.size.height - 1.0f, 300.0f, 1.0f)];
        NSLog(@"> >>>>>>>>>>>>>>>% f ", cell.frame.size.height);
        [seperateLine setBackgroundColor: [UIColor grayColor]];
        [cell.contentView addSubview: seperateLine];
        [выпуск seperateLine];
    }

    // ...
}

Я попытался проверить cell.frame.size.height, и, наконец, это было 44. И тогда я заменяю строку

UIView * seperateLine = [[UIView alloc] initWithFrame:CGRectMake(10.0f, cell.frame.size.height - 1.0f, 300.0f, 1.0f)];

до

UIView * seperateLine = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 31.0f, 300.0f, 1.0f)];

Это работает как первое изображение, показанное выше. Но the cell's height was still 44, они (которые мы просто не можем видеть полностью) все еще перекрываются. То, что я сделал, просто добавил separate line, где y позиция равна 32, но ее общая высота равна 44.


Итак что вы думаете об этом? :

Ответы [ 2 ]

3 голосов
/ 13 декабря 2011

Проблема:

В соответствии с HIG от Apple, область касания по умолчанию равна 44, поэтому по умолчанию все элементы управления имеют высоту 44.

вы определяете новый UITableViewCell внутри

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

, по умолчанию 44, и вы ничего не меняете в кадре UITableViewCell и возвращаете его.

Решение:

попробуйте установить размер кадра UITableViewCell, т.е. cell.frame = CGRectMake(0.0f, 0.0f, 320.0f, 32.0f)

2 голосов
/ 13 декабря 2011

Пожалуйста, обратитесь к документации и найдите tableView:heightForRowAtIndexPath: метод делегата.С помощью которого вы можете установить высоту для каждого ряда.Это может быть исправлено или может быть динамичным, а также в соответствии с вашими требованиями.

Надеюсь, это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...