0

重複の可能性:
UITableView create 4 square

uiTableview で 4 つのセルを 1 行に表示したいのですが、何も表示されません。セルの画像を教えてください。

前もって感謝します

 - (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

ここでは、必要なデモ アプリケーションを 1 つ作成し、コードの一部をここに挿入します。

- (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 に答える
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 に答える