2011-04-04 62 views
0

我困在一个小问题上。假设我有一个设备列表,分布在不同的位置。我在表格列表中输出所有这些设备。其中一些设备碰巧具有相同的位置。 是否有可能通过格式化这些单元格的粗体/双线来以某种方式对这些表单元格进行分组? 我知道多个自定义单元格的可能性,但我希望有人有一个更简单的方法:) 谢谢!分组一些表格单元格

回答

1

如果你想要的东西像下面

enter image description here

下面的代码添加到您的普通的tableview和每个位置返回部分按你的位置和行数,按您的设备的数量

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 5.0; 
} 
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    UIView* view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 2)]; 
    view.backgroundColor = [UIColor blackColor]; 
    return view; 
} 
+0

该死的,好想法! – Joetjah 2011-04-04 09:46:21

0

用于使组中的单元格可以使用组表。

+0

我不喜欢使用该表样机,因为它拆分设备......这只是为了表明他们在一起,而不是将他们全部分开。 – Joetjah 2011-04-04 08:28:06

1

不知道我理解你的问题,但你可以根据某些数据自定义任何单元格以寻找你想要的方式。

比方说,你设置你的设备的名称和位置,这样一个数组:

// keys in your device object 
NSArray *aKeys = [NSArray arrayWithObjects:@"name", @"location", nil]; 
// 1st device (in location 1) 
NSDictionary *device1 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Device 1", @"Location 1", nil] forKeys:aKeys]; 
// 2nd device (in location 2) 
NSDictionary *device2 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Device 2", @"Location 2", nil] forKeys:aKeys]; 
// 3rd device (in location 1 like device 1) 
NSDictionary *device3 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Device 3", @"Location 1", nil] forKeys:aKeys]; 
// array of devices 
NSArray *aDevices = [NSArray arrayWithObjects:device1, device2, device3, nil]; 

在你的tableView:(UITableView的*)电视 的cellForRowAtIndexPath:(NSIndexPath *)indexPath方法,你可以再检查并突出显示每个类型的位置。在这种情况下,我只是要一个彩色条添加到依赖于位置的颜色变化,像这样的小区的边缘:

if([[aDevices objectAtIndex:indexPath.row] objectForKey:@"location"] == @"Location 1"){ 
    // if location is Location 1 then we'll add an ORANGE colored strip to the left indicating location 1 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"location1Cell"]; 
    if(cell == nil) { 
     cell = [[[UITableViewCell alloc] 
       initWithFrame:CGRectZero reuseIdentifier:@"location1Cell"] autorelease]; 
     // colored strip 
     UIView *vwLoc = [[[UIView alloc] 
        initWithFrame:CGRectMake(0.0, 0.0, 5.0, 20.0)] autorelease]; 
     [vwLoc setBackgroundColor:[UIColor orangeColor]]; 
     [cell.contentView addSubview:vwLoc]; 
    } 
} else if([[aDevices objectAtIndex:indexPath.row] objectForKey:@"location"] == @"Location 2"){ 
    // if location is Location 2 then we'll add an YELLOW colored strip to the left indicating location 2 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"location2Cell"]; 
    if(cell == nil) { 
     cell = [[[UITableViewCell alloc] 
       initWithFrame:CGRectZero reuseIdentifier:@"location2Cell"] autorelease]; 
     // colored strip 
     UIView *vwLoc = [[[UIView alloc] 
          initWithFrame:CGRectMake(0.0, 0.0, 5.0, 20.0)] autorelease]; 
     [vwLoc setBackgroundColor:[UIColor yellowColor]]; 
     [cell.contentView addSubview:vwLoc]; 
    } 
} 
// if location 3, location 4, etc. ... 
+0

是的,这也是我的第一个想法,虽然Iphone_bharat发布的建议正是我正在寻找的! :) 非常感谢你的帮助 ;) – Joetjah 2011-04-04 09:47:32