uitableview中的单元格规范

时间:2012-07-31 06:45:11

标签: objective-c xcode ios5 uitableview

  

可能重复:
  UITableView create 4 square

我想在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方法

enter image description here

3 个答案:

答案 0 :(得分: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];

并查看是否有任何变化。

答案 1 :(得分: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;
}

答案 2 :(得分: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;
}