Как создать пользовательский вид разделов? - PullRequest
1 голос
/ 18 октября 2011

Предположим, я хочу выделить место на экране для нескольких прямоугольников, каждый из которых отвечает за хранение некоторых данных.Я думаю, что это будет TableView с 3 строками

Каким будет объект, представляющий внешнюю форму, в котором будет размещаться таблица-бокс?

/-----------\
| some txt  |
| more txt  |
| other txt |
\-----------/

/-----------\
| some txt  |
| more txt  |
| other txt |
\-----------/

Полагаю, я мог бы сделать этоUIButton, который нельзя щелкнуть, но поместить UITableView внутрь кажется неловким.

Как это делается в приложении Stocks?Есть раздел сверху и раздел снизу.

1 Ответ

2 голосов
/ 18 октября 2011

У вас есть разные заголовки для tableView: по одному для tableView, и вы можете иметь один для каждого раздела

Зеленый - это tableViewHeader, а синий показывает sectionHeaders.

enter image description here

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (headerView == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"DetailContactHeader" owner:self options:nil];
        headerView.nameLabel.text = [NSString stringWithFormat:@"%@ %@", 
                                                   [contact objectForKey:@"name"],
                                                   [contact objectForKey:@"familyname"]];
        if ([[contact allKeys] containsObject:@"pictureurl"]) {
            headerView.avatarView.image = [UIImage imageNamed:[contact objectForKey:@"pictureurl"]];
        }
    }
    [self.tableView setTableHeaderView: headerView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [[contact allKeys] count]-3;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView       
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 
                                       reuseIdentifier:CellIdentifier] autorelease];
    }

    id key = [self.possibleFields objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@", key];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [contact objectForKey:key]];
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView 
  heightForHeaderInSection:(NSInteger)section
{
    return 44.0;
}

-(UIView *) tableView:(UITableView *)tableView 
viewForHeaderInSection:(NSInteger)section
{
    UILabel *l = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
    l.backgroundColor = [UIColor clearColor];
    l.text= @"I am a Section Header";
    return l;
}

Вы найдете код этого приложения здесь: MyContacts

Для любого метода …header… существует соответствующий метод …footer….

Как это делается в приложении Stock, я просто могу догадаться: я думаю, что-то похожее.

Чтобы судить, является ли это правильным решением для вас, вы не предоставляете достаточно информации.Но я так думаю.

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