带有textView的单元格

时间:2011-06-03 10:44:32

标签: iphone objective-c

你好 我有一个单元格的表视图,我在这个单元格上创建了​​一个UITextView,用户可以编写他想要的东西,这是cellForRow中的代码:

static NSString *CellIdentifier = @"Cell";
UITextView *text;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    text = [[UITextView alloc]initWithFrame:CGRectMake(1, 5, 200, 140)];
    text.tag = 1;

    UIView *statusView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300, 150)];
    statusView.tag = 0;
    [statusView addSubview:text];
    [cell.contentView addSubview:statusView];
}
else {
    text = (UITextView *)[[cell.contentView viewWithTag:0] viewWithTag:1];
}

我想创建一个保存按钮,我想知道如何从该类中的另一个方法获取此UITextView中的文本。 THX

1 个答案:

答案 0 :(得分:2)

使用UIButton创建按钮并为其设置操作(方法),点击按钮时会调用该按钮。

-(void) buttonPressed:(id) sender
{
   //First get the cell (We know we have the only one cell).

   // Create an NSIndexPath to access the cell from UITableView.
    NSIndexPath *myIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];

   //Now We can access the cell using UITableView instance.
   UITableViewCell *myCell = [myTabelView cellForRowAtIndexPath:myIndexPath];

   //Now access the instance of UITextView using viewWithTag property of UIView.
   UITextView* myTextView = (UITextView*) [myCell viewWithTag:1];


   //Now you could get the text. :)
   NSString* myTextViewText = myTextView.text ;


}
相关问题