uitableview中的单元格规范[重复]

Posted

技术标签:

【中文标题】uitableview中的单元格规范[重复]【英文标题】:cell specification in uitableview [duplicate] 【发布时间】:2012-07-31 06:45:10 【问题描述】:

可能重复: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 方法

【问题讨论】:

@dasdom 但有不同的错误我需要答案 同一个问题被问了几次,你不会提高答案的质量。当答案无助于改善您的问题时。 @dasdom 好的,谢谢您的提示 @dasdom 这就是我所拥有的一切,请你帮帮我,我只想在表格的一行中有 4 个按钮或 4 个图片或 4 个单元格 【参考方案1】:

在我看来,在:

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];

看看有没有什么变化。

【讨论】:

我添加了我拥有的图片 你知道如何在列之间放置空格吗? 好吧,你有 Rog 的样本来正确设置你的细胞框架。将cell.contentView.size.height 更改为cell.contentView.frame.size.height。此外,第二行是aView,而不是aview!!【参考方案2】:

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

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

【讨论】:

也可以从bit.ly/T1Cy0N下载示例代码希望对你有帮助【参考方案3】:

你的方法有点乱。您的主要问题是如何为 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;

【讨论】:

我收到一个错误,我把它放在编辑中,请你检查一下

以上是关于uitableview中的单元格规范[重复]的主要内容,如果未能解决你的问题,请参考以下文章

UITableView“dequeueReusableCellWithIdentifier”导致基于单元格内容的动态单元格高度的单元格重叠?

UITableView 单元格折叠动画看起来很糟糕

如何减少/删除分组 UITableView 中的左/右手边距?

当 UITableView 单元格中的 UITextView 大小增加时,节标题重复

将 UIButton 放入 UITableView

Swift:单元格 TableviewCell 中的手势不起作用