【问题标题】:Add textfield and checkbox on tableview在 tableview 上添加文本字段和复选框
【发布时间】:2011-09-15 12:36:02
【问题描述】:

在我的 Iphone 应用程序中,我必须在 tableview 的两行添加文本字段,并在一行添加一个复选框。谁能给我一个可以帮助我的例子。我是 Iphone 新手,不知道如何开始。

提前谢谢..

【问题讨论】:

  • 你应该先学习 UITableView 的自定义单元格...
  • 你试过什么?您是通过编程方式还是使用 IB 创建表?
  • 我使用 IB 创建了表格..我尝试添加一个文本字段但没有任何反应
  • 这样的东西,它应该工作吗? if(indexPath.row==1) { UITextField *labl=[[UITextField alloc] init]; labl.text=@"数据"; [cell.contentView addSubview:labl]; }
  • 您到底想做什么?请清除您的问题。你想做什么?

标签: iphone ios checkbox tableview textfield


【解决方案1】:

嘿,在你的 cellForRowAtindexPath 中试试这个

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

{

UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"CellID%d%d",indexPath.section,indexPath.row]];
if (!cell){
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:[NSString stringWithFormat:@"CellID%d%d",indexPath.section,indexPath.row]] autorelease];  
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

if(indexPath.row == 0){
            //  NSString *daytime = [[NSUserDefaults standardUserDefaults] objectForKey:@"day"];
            UITextField *text0 = [[UITextField alloc] initWithFrame:CGRectMake(20, 12, 120, 22)];
            text0.textAlignment = UITextAlignmentLeft;
            text0.backgroundColor = [UIColor whiteColor];
            text0.borderStyle = UITextBorderStyleNone;
            text0.userInteractionEnabled = FALSE;
            text0.font = [UIFont boldSystemFontOfSize:17];
            text0.textColor = [UIColor blackColor];
            text0.delegate = self;
            text0.text = @"Type de Parteneria";
            [cell addSubview:text0];

            UITextField *text1 = [[UITextField alloc] initWithFrame:CGRectMake(120, 12, 160, 20)];
            text1.textAlignment = UITextAlignmentRight;
            text1.borderStyle = UITextBorderStyleNone;
            text1.backgroundColor = [UIColor whiteColor];
            text1.userInteractionEnabled = TRUE;
            text1.font = [UIFont boldSystemFontOfSize:16];
            text1.textColor = [UIColor colorWithRed:114.0f/255.0f green:136.0f/255.0f blue:165.0f/255.0f alpha:1.0];
            text1.delegate = self;

            [cell addSubview:text1];

    }
    else if(indexPath.row == 1){
            //  NSString *daytime = [[NSUserDefaults standardUserDefaults] objectForKey:@"day"];
            UITextField *text0 = [[UITextField alloc] initWithFrame:CGRectMake(20, 12, 120, 22)];
            text0.textAlignment = UITextAlignmentLeft;
            text0.backgroundColor = [UIColor whiteColor];
            text0.borderStyle = UITextBorderStyleNone;
            text0.userInteractionEnabled = FALSE;
            text0.font = [UIFont boldSystemFontOfSize:17];
            text0.textColor = [UIColor blackColor];
            text0.delegate = self;
            text0.text = @"Audi R8";
            [cell addSubview:text0];


            UIButton *signBtn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
        signBtn.frame = CGRectMake(15, 170, 290, 35);
        [signBtn setTitle:@"Sign Up" forState:UIControlStateNormal];
        [signBtn setBackgroundImage:[UIImage imageNamed:@"CheckBox.png"] forState:UIControlStateNormal];
        [signBtn setFont:[UIFont boldSystemFontOfSize:17]];
        [signBtn addTarget:self action:@selector(checkPressed) forControlEvents:UIControlEventTouchDown];
        [cell addSubview:signBtn];
    }
   return cell;

}

如果遇到问题,请给我留言

【讨论】:

  • 哦!最后我的错误你必须写返回单元格;
【解决方案2】:

根据您的评论试试这个:

if(indexPath.row==1)
{
  UITextField *labl=[[UITextField alloc] init];
  // where is your text field's frame ???? 
  labl.frame = CGRectMake(10,10,50,50);
  labl.text=@"data"; 
  [cell addSubview:labl]; 
}

【讨论】:

  • 请不要这样做。它可能有效,但以这种方式添加的视图通常在单元格转换进入和退出编辑模式期间适当定位,并且通常是hacky。 UITableViewCell contentView 属性是专门为向单元格添加子视图而创建的。
猜你喜欢
  • 2012-05-28
  • 1970-01-01
  • 2014-05-21
  • 1970-01-01
  • 2020-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多