0

可能重复:
UITableView 创建 4 个正方形

我想在 uiTableview 的一行中有 4 个单元格,但什么也没有出现,我不知道你为什么要帮我这个单元格的图片

提前致谢

 - (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];

NSString *monKey = @"Monday";
NSString *tueKey = @"Tuesday";
NSString *wedKey = @"Wednday";
NSString *thuKey = @"Thusday";
NSString *friKey = @"Friday";
NSString *satKey = @"Satuarday";
NSString *sunKey = @"Sunnday";

[contents setObject:[NSArray arrayWithObjects:@"Work Time", @"Absence", nil] forKey:monKey];
[contents setObject:[NSArray arrayWithObjects:@"Compensation", @"Work Time", @"Absence", nil] forKey:wedKey];
[contents setObject:[NSArray arrayWithObjects:@"Compensation", @"Work Time", nil] forKey:tueKey];
[contents setObject:[NSArray arrayWithObjects:@"Compensation", @"Work Time", nil] forKey:thuKey];
[contents setObject:[NSArray arrayWithObjects:@"Compensation", @"Work Time", nil] forKey:friKey];
[contents setObject:[NSArray arrayWithObjects:@"Compensation", @"Work Time", nil] forKey:satKey];
[contents setObject:[NSArray arrayWithObjects:@"Compensation", @"Work Time", nil] forKey:sunKey];

[keys addObject:tueKey];
[keys addObject:monKey];
[keys addObject:wedKey];
[keys addObject:thuKey];
[keys addObject:friKey];
[keys addObject:satKey];
[keys addObject:sunKey];



[self setSectionKeys:keys];
[self setSectionContents:contents];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
target:self action:@selector(addNewItem)];
self.navigationItem.rightBarButtonItem = rightButton;
}

cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithFrame:CGRectZero]; 
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;

int column = 4;
for (int i=0; i<column; i++) {

    UIImageView *aBackgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(32+184*i,10, 167,215)];
    aBackgroundImageView.tag = (column*indexPath.row)+i;
    [cell.contentView addSubview:aBackgroundImageView];
   // [aBackgroundImageView release];
}
return cell;
}

编辑 1

这是我添加此行时的图片

 aBackgroundImageView.backgroundColor = [UIColor redColor];

编辑 2:使用 Rog 方法

在此处输入图像描述

4

3 回答 3

0

你的方法有点乱。您的主要问题是您如何设置 UIImageView 实例的框架原点,因为它们 1)对于单元格而言太大 2)超出单元格边界。

试试这个(ps:可能有错别字,因为我已经把它写在我的头上):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    int numberOfColumns = 4;
    CGSize gridSize = CGSizeMake(cell.contentView.frame.size.width / numberOfColumns, cell.contentView.frame.size.height / 2); // Height size is arbitrary here, width is obtained by dividing total width by number of columns
    for (int i=0; i < column; i++) 
    {
         UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(gridSize.width * i, cell.contentView.frame.size.height/2 - gridSize.height/2, gridSize.width, gridSize.height)];
         aview.backgroundColor = [UIColor colorWithWhite:1.0 alpha:1/i+1]; // This is just so you can see the different gridCells within the cell
         [cell.contentView addSubview:aView];
         [aView release];
    }
    return cell;
}
于 2012-07-31T07:14:42.250 回答
0

在我看来,在:

for (int i=0; i<column; i++) {

  UIImageView *aBackgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(32+184*i,10, 167,215)];
  aBackgroundImageView.tag = (column*indexPath.row)+i;
  [cell.contentView addSubview:aBackgroundImageView];
  // [aBackgroundImageView release];
}

您没有为您的观点提供内容...

所以这应该解释为什么你什么都看不到......尝试添加:

aBackgroundImageView.backgroundColor = [UIColor redColor];

看看是否有任何变化。

于 2012-07-31T06:58:41.473 回答
0

在这里,我创建了一个您想要的演示应用程序,并将一些代码放入此处。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSUInteger devide=[self.tableArray count] / 3;

    if ([self.tableArray count] % 3 &gt;0 ) {

        devide+=1;

    } else {

        devide=devide;

    }
    return devide;
}

和 cellForRowAtIndexPath。

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell==nil){
        cell=[[[NSBundle mainBundle] loadNibNamed:@"CCell" owner:self options:nil] objectAtIndex:0];
        UIButton *imgV1=(UIButton*)[cell viewWithTag:1];
        UIButton *imgV2=(UIButton*)[cell viewWithTag:2];
        UIButton *imgV3=(UIButton*)[cell viewWithTag:3];
        [imgV1 addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchDown];
        [imgV2 addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchDown];
        [imgV3 addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchDown];
        HJManagedImageV *img1=[[HJManagedImageV alloc] initWithFrame:CGRectMake(0, 0, imgV1.frame.size.width, imgV1.frame.size.height)];
        HJManagedImageV *img2=[[HJManagedImageV alloc] initWithFrame:CGRectMake(0, 0, imgV2.frame.size.width, imgV2.frame.size.height)];
        HJManagedImageV *img3=[[HJManagedImageV alloc] initWithFrame:CGRectMake(0, 0, imgV3.frame.size.width, imgV3.frame.size.height)];
        img1.tag=img2.tag=img3.tag=10000;

        img1.backgroundColor=img2.backgroundColor=img3.backgroundColor=[UIColor clearColor];
        [imgV1 addSubview:img1];
        [imgV2 addSubview:img2];
        [imgV3 addSubview:img3];
    }

    int x=indexPath.row;
    x=x*3;

    UIButton *imgV1=(UIButton*)[cell viewWithTag:1];
    UIButton *imgV2=(UIButton*)[cell viewWithTag:2];
    UIButton *imgV3=(UIButton*)[cell viewWithTag:3];

    HJManagedImageV *img1=(HJManagedImageV*)[imgV1 viewWithTag:10000];
    HJManagedImageV *img2=(HJManagedImageV*)[imgV2 viewWithTag:10000];
    HJManagedImageV *img3=(HJManagedImageV*)[imgV3 viewWithTag:10000];

    [img1 clear];
    [img2 clear];
    [img3 clear];

    [imgV1 setTitle:[self.tableArray objectAtIndex:x] forState:UIControlStateDisabled];
    img1.url=[NSURL URLWithString:[self.tableArray objectAtIndex:x]];
    [img1 showLoadingWheel];
    [GlobalCacheManager manage:img1];

    if((x+1) &gt;= self.tableArray.count){
        imgV2.hidden=imgV3.hidden=YES;
        [imgV2 setBackgroundImage:nil forState:UIControlStateNormal];
        [imgV3 setBackgroundImage:nil forState:UIControlStateNormal];
    } else if( (x+2)&gt;=self.tableArray.count) {
        img2.url=[NSURL URLWithString:[self.tableArray objectAtIndex:x+1]];
        [img2 showLoadingWheel];
        [GlobalCacheManager manage:img2];
        imgV1.hidden=imgV2.hidden=YES;
        imgV3.hidden=YES;
        [imgV2 setTitle:[self.tableArray objectAtIndex:x+1] forState:UIControlStateDisabled];
        [imgV3 setBackgroundImage:nil forState:UIControlStateNormal];
    } else {
        [img2 showLoadingWheel];
        [img3 showLoadingWheel];
        img2.url=[NSURL URLWithString:[self.tableArray objectAtIndex:x+1]];
        [GlobalCacheManager manage:img2];
        img3.url=[NSURL URLWithString:[self.tableArray objectAtIndex:x+2]];
        [GlobalCacheManager manage:img3];
        imgV2.hidden=imgV3.hidden=NO;
        [imgV2 setTitle:[self.tableArray objectAtIndex:x+1] forState:UIControlStateDisabled];
        [imgV3 setTitle:[self.tableArray objectAtIndex:x+2] forState:UIControlStateDisabled];
    }

    return cell;
}
于 2012-07-31T07:20:45.927 回答