2013-02-14 72 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; 
}