0

我在这里有一个标准的表格视图,我希望能够在每个显示背景的不可见/隐藏单元格之间添加一个空格。正确的做法是什么?

这是我正在使用的代码:

- (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;
}
4

3 回答 3

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;
    }
于 2013-05-02T06:47:49.493 回答
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;
    }
}
于 2013-12-05T03:15:24.453 回答
0

首先让我说我没有阅读您的所有代码。这就是说我已经做到了这一点。第一种方法是在 heightForCell 中为上面应该有间隙的单元格声明一个更大的值。cell.contentView 可能需要设置其 clipToBounds。我相信在这种情况下,应该通过设置 cell.frame.size (原点为 0,0)来设置单元格大小,并且 contentView 和 backgroundview 的大小也正确。

第二种方法是插入空单元格 - 具有不同重用标识符的单元格,它们只是空单元格,所有内容都设置为具有清晰的颜色。

于 2012-09-21T02:47:20.250 回答