【问题标题】:How to create custom view sections?如何创建自定义视图部分?
【发布时间】:2011-10-18 19:15:10
【问题描述】:

假设我想在屏幕上为几个矩形分配一些空间,每个矩形负责保存一些数据。我想这将是一个 3 行的 TableView

代表外部形状的对象是什么,盒子桌子将坐在那里?

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

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

我想我可以让它成为一个不可点击的 UIButton,但是把 UITableView 放在里面似乎很尴尬。

它是如何在股票应用程序中完成的?顶部有一个部分,底部有一个部分。

【问题讨论】:

    标签: objective-c cocoa-touch


    【解决方案1】:

    每个 tableView 有不同的标题:tableView 一个,每个部分可以有一个

    绿色是tableViewHeader,蓝色是sectionHeaders。

    -(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 应用程序中它是如何完成的,我只能猜测:我认为有些相似。

    要判断这是否是适合您的解决方案,您没有提供足够的信息。但我想是的。

    【讨论】: