开发者

iphone sdk - TableView Cell with custom TextView - Get New Data

I have a tableview cell with a custom textview in it, I am now left wondering how do I possibly access the text in the textbox after text has been edited/added.

I would normally know how this is done when I draw the textfield through IB, but my textviewcell is dynamically drawn.

(i.e. I would like to capture the data that is updated in detailLabel.text)

Here is the related code to adapt to your answer. Thanks again!

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


    //Big Text Box
    UITextView *detailLabel = [[UITextView alloc] initWithFrame:CGRectMake(30, 80, 500, 150)];
    detailLabel.tag = 20;
    [cell.contentView addSubview:detailLabel];
    detailLabel.layer.borderWidth = 1;
    detailLabel.layer.borderColor = [[UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.9] CGColor];
    detailLabel.layer.cornerRadius = 10;
    detailLabel.font = [UIFont fontWithName:@"Helvetica" size:17];
    [detailLabel release];

} 

UITextView * detailLabel = (UITextView *) [cell.contentView viewWithTag:20];

switch (indexPath.row) {

case 0:
    detailLabel.text = @"no";
    break;
default:
    detailLabel.text = [NSStri开发者_JS百科ng stringWithFormat:@"%d", indexPath.row];
    detailLabel.hidden = NO;    

}


You'll want to listen for messages sent by the UITextView. So look to implement the UITextViewDelegate protocol and register the implementing class with the detailLabel using its delegate property.

When your delegate is notified of a change in the UITextView you'll have to identify the cell which currently owns the UITextView. In doing so you must remember that cells can be reused.

I would start by looking at the following method in the UITableView class:

- (NSArray *)visibleCells

We know that if the user has made a change to the UITextView's contents it must currently be on screen and therefore be present in the pre-mentioned array. To find it we use the pointer to the UITextView which changed (it's a parameter in the textViewDidChange protocol method). So simply iterate over the visibleCells array and retrieve the UITextView and compare it against the UITextView which changed.

- (void)textViewDidChange:(UITextView *)textView {
....
cellsLabel = (UITextView *) [cell.contentView viewWithTag:20];
if (cellsLabel == textView)
...

You'll now have a handle to a cell in the UITableView. To find the index use the following UITableView method:

- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜