【问题标题】:Adding a view to a UITableViewCell's contentView, places the view in the wrong place向 UITableViewCell 的 contentView 添加视图,将视图放置在错误的位置
【发布时间】:2013-11-29 17:18:03
【问题描述】:

我似乎无法理解子视图的工作原理。我有一个UITextfield,我想将它添加到UITableViewCell 的内容视图中,所以我这样做:

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

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

   if (cell==nil) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    }    // Configure the cell...
    UITextField *tf = [textFieldsArray objectAtIndex:[indexPath row]];
    tf.frame = CGRectMake(0, 0, 100, 12);
    [cell.contentView addSubview:tf];
    return cell;
}

我假设当我将它作为子视图添加到内容视图时,通过使文本字段框架原点 (0,0),它会将其放置在位置 (0,0) 的内容视图中,但这并没有发生,它实际上是把它放在整个单元格(单元格的最左上部分)的位置(0,0)。

除非这在 iOS7 中有所改变,否则我认为内容视图是这样的:


(来源:idev101.com

如果是这样,有没有办法我可以将它实际放置在 contentView 中,而不必每次都手动定义偏移量?

【问题讨论】:

  • contentView 的左上角和单元格是相同的,除非您处于编辑模式。
  • 啊,谢谢,我觉得自己很愚蠢!
  • @TylerDurden 没关系。有时,当你看得太久时,很难看到明显的东西。享受。 :)
  • 顺便说一句 - 当您想回复某人时,请务必在前面指定完整的用户名 @ 符号。

标签: ios uitableview ios7 uitextfield


【解决方案1】:
     Try This 


       if (cell==nil) 
        {
        cell = [[[UITableViewCell alloc]initWithStyle:
        UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];

        UIImageView *img=[[UIImageView alloc]init];
        img.tag=100;
        img.layer.cornerRadius = 3;
        img.layer.masksToBounds = YES;
        [cell addSubview:img];


        UILabel *lbl= [[UILabel alloc]init];
        lbl.backgroundColor= [UIColor clearColor];
        lbl.text=@"ABC";
        lbl.textAlignment=NSTextAlignmentLeft;
        lbl.textColor=[UIColor blackColor];
        lbl.tag=200;
       [cell addSubview:lbl];


     }
img1=(UIImageView *)[cell viewWithTag:100];

NSArray *imgDataArr = [[NSArray alloc] initWithArray:[[arrFields objectAtIndex: storyIndex] objectForKey: @"image"]];

for (UIImage *image in imgDataArr)
{
    if (image)
        img1.image =image;
}
[cell addSubview:img1];


UILabel *lbl1 = (UILabel*)[cell viewWithTag:200];
[cell addSubview:lbl1];
lbl1.numberOfLines=3;
lbl1.text=[[arrFields objectAtIndex: storyIndex] objectForKey: @"Item_No"];

【讨论】:

    猜你喜欢
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多