我想在表视图中显示单元格,它们之间有一些差距

时间:2010-08-26 19:40:57

标签: iphone xcode uitableview

-  (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [topics count];
    }

    - (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {

        CGRect cellFrame = CGRectMake(0,0,320,45);  
        UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:cellFrame reuseIdentifier:cellIdentifier] autorelease];

        CGRect bgFrame = CGRectMake(10,5,300,35);   
        UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"list_bg_up.png"]];
        backgroundView.frame = bgFrame;
        [cell addSubview:backgroundView];

        UILabel *txtLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,10,250,25)];
        txtLabel.textColor=[UIColor whiteColor];
        txtLabel.backgroundColor = [UIColor clearColor];
        txtLabel.tag = 1;
        [cell addSubview:txtLabel];

        return cell;
    }   

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

        UITableViewCell *cell;

        static NSString *kDisplayCell_ID = @"DisplayCellID";

        //cell = [self.tableView dequeueReusableCellWithIdentifier:kDisplayCell_ID];
        cell = [tableView dequeueReusableCellWithIdentifier:kDisplayCell_ID];

        if(cell == nil) {
            cell = [self getCellContentView:kDisplayCell_ID];
        }

        aTopic = [topics objectAtIndex:indexPath.row];
        UILabel *txtLabel = (UILabel *)[cell viewWithTag:1];
        txtLabel.text = aTopic.description;

        return cell;
    }

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

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        ResourcesViewController *resViewController = [[ResourcesViewController alloc] initWithNibName:@"ResourcesViewController" bundle:[NSBundle mainBundle]];
        resViewController.aTopic = [topics objectAtIndex:indexPath.row];
        [self.navigationController pushViewController:resViewController animated:YES];
        [resViewController release];
    }

如果我想改变单元格之间的空间,我可以在此代码中进行更改....

现在有了这个代码,我能够显示有一些差距的细胞,但我不明白差距是如何定义的......

2 个答案:

答案 0 :(得分:2)

如果您正在谈论单元格的垂直高度,请更改此代码:

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

如果您正在讨论单元格中数据之间的水平空间,则需要在此处修改CGRect:

   CGRect cellFrame = CGRectMake(0,0,320,45);  
        UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:cellFrame reuseIdentifier:cellIdentifier] autorelease];

        CGRect bgFrame = CGRectMake(10,5,300,35);   
        UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"list_bg_up.png"]];
        backgroundView.frame = bgFrame;
        [cell addSubview:backgroundView];

        UILabel *txtLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,10,250,25)];

下次再详细说明你的要求。不要忘记接受你问的问题的答案。干杯

答案 1 :(得分:0)

我想也许你需要看一下这个问题:

  

How to give space between two cells in tableview?