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

私はあなたのコードをすべて読んでいないと言うことから始めましょう。それは私がまさにこれをしたと言った。最初のアプローチは、heightForCell で、ギャップが必要な位置よりも上のセルに大きな値を宣言することです。cell.contentView には、clipToBounds を設定する必要がある場合があります。この場合、cell.frame.size (原点は 0,0) を設定してセル サイズを設定し、contentsView と backgroundview も適切なサイズに設定する必要があると思います。

2 番目の方法は、空のセルを挿入することです。つまり、別の再利用識別子を持つセルで、すべてが明確な色に設定された単なる空のセルです。

于 2012-09-21T02:47:20.250 に答える
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 に答える