2013-02-14 45 views
0

我以编程方式在uitableviewcontroller内创建了uitextview,但我无法编辑textview。下面就以我的表布局的概述:在UITableViewController单元格内创建可编辑的UITextView

ROW1:的UILabel

行2:不可编辑的UITextView

ROW3:的UILabel

ROW4:可编辑的UITextView

这里的我在做什么:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UILabel *label = nil; 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if(indexPath.row%2==0) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 50)]; 
    [label setBackgroundColor:[self.cellBackgroundColor objectAtIndex:indexPath.row]]; 
    [label setText:[self.celltitle objectAtIndex:indexPath.row]]; 
    label.font=[UIFont fontWithName:[self.cellFontName objectAtIndex:indexPath.row] size:[[self.cellFontSize objectAtIndex:indexPath.row] integerValue]]; 

    label.textAlignment = NSTextAlignmentLeft; 
    [[cell contentView] addSubview:label]; 
} 
else{ 

    UITextView *messageBox= [[UITextView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 150)]; 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    cell.userInteractionEnabled=YES; 
    if(indexPath.row==3) 
    { 
     [messageBox setEditable:YES]; 
[messageBox setUserInteractionEnabled:YES]; 
     messageBox.delegate=self; 
     messageBox.editable=YES; 
    } 
    [cell.contentView addSubview: messageBox]; 


} 
return cell;} 

我也设置了textviewdelegat在头文件中的文本和textviews应该是方法,但row4的textview仍然是不可编辑的...任何帮助?

+0

做委托方法应对? – Luke 2013-02-14 13:16:08

+0

没有..委托方法没有响应.. – Shaunak 2013-02-14 13:19:25

+0

你有.h正确的链接,你添加了? – 2013-02-14 13:23:43

回答

2

除了使这些URL setEditable和setUserInteractionEnabled工作,你也必须确保这些属性在你的UITableViewController启用以及由于UITextView嵌套在它内!

[tableView setUserInteractionEnabled:YES]; 
[cell setUserInteractionEnabled:YES]; 
[messageBox setEditable:YES]; 
[messageBox setUserInteractionEnabled:YES]; 

(*注意,您的tableView和TableViewCell都具有相同的名字从这个功能,所以我愿意把这些代码在其他地方,但我说这上面只是柜面)

1

试试这个..也许它会帮助you..Its我

if(indexPath.row==3) 
    { 
     messageBox.delegate=self; 
     [messageBox setEditable:YES]; 
     [messageBox setUserInteractionEnabled:YES]; 
     messageBox.editable=YES; 
     [cell addSubview: messageBox]; 
    } 
+0

仍然没有工作.. – Shaunak 2013-02-14 13:21:18

+0

[cell addSubview:messageBox];在支架中写下这一行 – 2013-02-14 13:28:28

-1

您必须初始化cell's内容视图,然后再使用它。试试这个:

cell.contentView = [[UIView alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width, 150)]; 
0

我想你的code..Its工作对我来说..看到的变化波纹管

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UILabel *label = nil; 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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


    if(indexPath.row%2==0) 
    { 
     label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 50)]; 
      [label setBackgroundColor:[self.cellBackgroundColor objectAtIndex:indexPath.row]]; 
     [label setText:[self.celltitle objectAtIndex:indexPath.row]]; 
     label.font=[UIFont fontWithName:[self.cellFontName objectAtIndex:indexPath.row] size:[[self.cellFontSize objectAtIndex:indexPath.row] integerValue]]; 

     label.textAlignment = NSTextAlignmentLeft; 
     [[cell contentView] addSubview:label]; 
    } 
    else{ 

     UITextView *messageBox= [[UITextView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, 100)]; 

     cell.userInteractionEnabled=YES; 
     if(indexPath.row==1) 
     { 
      // Uneditable UITextview 
      messageBox.editable=NO; 
     } 
     [cell.contentView addSubview: messageBox]; 

    } 
} 
return cell; 
} 
相关问题