2011-04-04 66 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