2012-09-20 56 views
0

我在這裏有一個標準的tableview,我希望能夠在顯示背景的不可見/隱藏的每個單元格之間添加一個空格。什麼是正確的做法?iOS在單元格之間創建一個不可見的空間

這裏是我使用的代碼:

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

static NSString *CellIdentifier = @"Cell"; 
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row]; 

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

    // Format each frame that holds each piece of content in each row 

    CGRect countyImageFrame = CGRectMake(275, 6, 18, 18); 
    UIImageView *countyImageLabel = [[UIImageView alloc] initWithFrame:countyImageFrame]; 
    countyImageLabel.tag = 0016; 
    countyImageLabel.backgroundColor = BG_COLOR 
    [cell.contentView addSubview:countyImageLabel]; 

    CGRect callTypeImageFrame = CGRectMake(275, 30, 18, 18); 
    UIImageView *callTypeImageLabel = [[UIImageView alloc] initWithFrame:callTypeImageFrame]; 
    callTypeImageLabel.tag = 0017; 
    callTypeImageLabel.backgroundColor = BG_COLOR 
    [cell.contentView addSubview:callTypeImageLabel]; 

    CGRect callTypeFrame = CGRectMake(5, 2, 175, 15); 
    UILabel *callTypeLabel = [[UILabel alloc] initWithFrame:callTypeFrame]; 
    callTypeLabel.tag = 0011; 
    callTypeLabel.backgroundColor = BG_COLOR 
    callTypeLabel.numberOfLines = 2; 
    callTypeLabel.font = [UIFont boldSystemFontOfSize:12]; 
    [cell.contentView addSubview:callTypeLabel]; 

    CGRect locationFrame = CGRectMake(5, 21, 175, 10); 
    UILabel *locationLabel = [[UILabel alloc] initWithFrame:locationFrame]; 
    locationLabel.tag = 0014; 
    locationLabel.backgroundColor = BG_COLOR 
    locationLabel.numberOfLines = 2; 
    locationLabel.font = [UIFont systemFontOfSize:10]; 
    [cell.contentView addSubview:locationLabel]; 

    CGRect unitsFrame = CGRectMake(3, 40, 175, 10); 
    UILabel *unitsLabel = [[UILabel alloc] initWithFrame:unitsFrame]; 
    unitsLabel.tag = 0012; 
    unitsLabel.backgroundColor = BG_COLOR 
    unitsLabel.textColor = [UIColor blackColor]; 
    unitsLabel.font = [UIFont italicSystemFontOfSize:10]; 
    [cell.contentView addSubview:unitsLabel]; 

    CGRect stationFrame = CGRectMake(185 , 28, 85, 20); 
    UILabel *stationLabel = [[UILabel alloc] initWithFrame:stationFrame]; 
    stationLabel.tag = 0013; 
    stationLabel.backgroundColor = BG_COLOR 
    stationLabel.font = [UIFont boldSystemFontOfSize:12]; 
    [cell.contentView addSubview:stationLabel]; 

    CGRect callnumberFrame = CGRectMake(185 , 5, 80, 20); 
    UILabel *callnumberLabel = [[UILabel alloc] initWithFrame:callnumberFrame]; 
    callnumberLabel.tag = 0015; 
    callnumberLabel.backgroundColor = BG_COLOR 
    callnumberLabel.font = [UIFont boldSystemFontOfSize:12]; 
    [cell.contentView addSubview:callnumberLabel]; 

} 

// Display content in each cell 

if ([currentCall.county isEqualToString:@"W"]) { 
    UIImage *countyImage = [UIImage imageNamed:@"blue.png"]; 
    UIImageView *countyImageLabel = (UIImageView *)[cell.contentView viewWithTag:0016]; 
    countyImageLabel.image = countyImage; 
} 
else { 
    UIImage *countyImage = [UIImage imageNamed:@"green.png"]; 
    UIImageView *countyImageLabel = (UIImageView *)[cell.contentView viewWithTag:0016]; 
    countyImageLabel.image = countyImage; 
} 

if ([currentCall.callType isEqualToString:@"F"]) { 
    UIImage *callTypeImage = [UIImage imageNamed:@"red.png"]; 
    UIImageView *callTypeImageLabel = (UIImageView *)[cell.contentView viewWithTag:0017]; 
    callTypeImageLabel.image = callTypeImage; 
} 
else { 
    UIImage *callTypeImage = [UIImage imageNamed:@"yellow.png"]; 
    UIImageView *callTypeImageLabel = (UIImageView *)[cell.contentView viewWithTag:0017]; 
    callTypeImageLabel.image = callTypeImage; 
} 

UILabel *callTypeLabel = (UILabel *)[cell.contentView viewWithTag:0011]; 
callTypeLabel.text = [currentCall currentCallType]; 

UILabel *locationLabel = (UILabel *)[cell.contentView viewWithTag:0014]; 
locationLabel.text = [currentCall location]; 

UILabel *unitsLabel = (UILabel *)[cell.contentView viewWithTag:0012]; 
unitsLabel.text = [currentCall units]; 

NSString *station = [@"Station: " stringByAppendingString:currentCall.station]; 

UILabel *stationLabel = (UILabel *)[cell.contentView viewWithTag:0013]; 
stationLabel.text = station; 

NSString *callnumber = [@"Call No: " stringByAppendingString:currentCall.callnumber]; 

UILabel *callnumberLabel = (UILabel *)[cell.contentView viewWithTag:0015]; 
callnumberLabel.text = callnumber; 

cell.backgroundColor = BG_COLOR 

return cell; 
} 

// Set how tall each cell is 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
return 55; 
} 

回答

0

讓我先說我沒看過所有的代碼開始。這就是說我已經完成了這個。第一種方法是在heightForCell中爲上面的單元格聲明一個更大的值,該單元格應該是缺口的位置。 cell.contentView可能需要設置其clipToBounds。我相信在這種情況下,應該通過設置cell.frame.size(原點爲0,0)來設置單元大小,並且也適當調整contentsView和backgroundview的大小。

第二種方法是插入空單元格 - 具有不同重用標識符的單元格,並且這些單元格只是空單元格,其中的所有內容都設置爲具有清晰的顏色。

+0

我採取了第三種方法,只是在每個單元格中創建一個視圖,比單元格高度小,然後創建分離的視圖。然後將所有內容作爲子視圖添加到視圖中。很簡單。 –

0

只需使用的伎倆讓每個細胞之間的空間..

static NSString *CELL_ID2 = @"SOME_STUPID_ID2"; 
    // even rows will be invisible 
    if (indexPath.row % 2 == 1) 
    { 
     UITableViewCell * cell2 = [tableView dequeueReusableCellWithIdentifier:CELL_ID2]; 

     if (cell2 == nil) 
     { 
      cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
              reuseIdentifier:CELL_ID2]; 
      [cell2.contentView setAlpha:0]; 
      [cell2 setUserInteractionEnabled:NO]; // prevent selection and other stuff 
     } 




     return cell2; 
    } 
0

我只是用不同的高度和細胞之間的交替顏色。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 

    if (indexPath.row % 2 == 1) 
    { 
     static NSString *CellIdentifier = @"cellID1"; 
     UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
              reuseIdentifier:CellIdentifier]; 
     } 

     cell.backgroundColor = [UIColor colorWhite]; 

     return cell; 

    } else { 

     static NSString *CellIdentifier2 = @"cellID2"; 
     UITableViewCell *cell2 = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; 
     if (cell2 == nil) { 
      cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
              reuseIdentifier:CellIdentifier2]; 
     } 
     cell2.backgroundColor = [UIColor clearColor]; 

     return cell2; 
    } 

} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row % 2 == 1) { 
     return 40.0; 
    } else { 
     return 2.0; 
    } 
} 
相關問題