如何在UITableView - iOS4中单击行时加载新的详细信息视图时清除以前的视图

时间:2011-10-29 19:53:06

标签: ios4

当单击表视图中的行时,我需要显示具有多个标签的详细视图控制器。我正在显示的标签是基于内容的动态高度,正在从iPhone的内部日历中读取。我已经能够根据表视图中的哪一行选择加载详细视图。我的问题是,单击新行时,前一个详细信息视图不会被清除。特别是在我之前的详细信息视图具有较长标签并且新详细信息视图具有较短标签的情况下,我可以在新标签下方看到先前标签的内容。在使用新行的数据重新加载之前,如何清除整个Detail View控制器。以下是我正在使用的代码:

RootViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

if (mdController == nil) {
    MtgDetailController *aController = [[MtgDetailController alloc] initWithNibName:@"MtgDetailController" bundle:nil];
    self.mdController = aController;
    [aController release];        
}
self.mdController.mtgIndex = indexPath.row;
//    mdController.mtgIndex = indexPath.row;
//    [mdController setMtgIndex:indexPath.row];
//    NSInteger temp = indexPath.row;

UIActionSheet *action = [[UIActionSheet alloc]
                         initWithTitle:@"Select an Option" 
                         delegate:self 
                         cancelButtonTitle:@"Cancel" 
                         destructiveButtonTitle:nil 
                         otherButtonTitles:@"Open Meeting", @"Dial into meeting", nil];

[action showInView:self.parentViewController.view];
[action release];
}

MtgDetailController.m(这是详情视图控制器)

-(void) viewDidAppear:(BOOL)animated { 

//    [self reloadInputViews];
Smart_MeetingAppDelegate *myDelegate = (Smart_MeetingAppDelegate *)[[UIApplication   sharedApplication]delegate];
CGRect scrollViewFrame = CGRectMake(0, 0, 320, 460);
NSString *title = [myDelegate.titles objectAtIndex:self.mtgIndex];
NSString *mtg_time = [myDelegate.mtg_times objectAtIndex:self.mtgIndex];
NSString *loc = @"Conf Room 123";
//    detail = @"test mtg test mtg. Dial number: 3334445555, (333)444-5555, 333-333-5555. ID: 4455333344, Password: 6576567";
detail = [myDelegate.detail_array objectAtIndex:self.mtgIndex];

NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:@"\\b[0-9]+(\\-)?(\\))?(\\.)?(\\s)?([0-9]+)?(\\-)?(\\.)?(\\s)?([0-9]+)?(\\-)?(\\.)?(\\s)?([0-9]+)?\\b" options:NSRegularExpressionCaseInsensitive error:nil];
NSArray* matches = [regex matchesInString:detail options:0 range:NSMakeRange(0, [detail length])];
[regex release];

unichar chr[1] = {'\n'};
NSString *singleCR = [NSString stringWithCharacters:(const unichar *)chr length:1];
NSString *titleRow = [NSString stringWithFormat:@"%@ %@ %@", title, singleCR, mtg_time];

NSMutableAttributedString *attrTitle = [NSMutableAttributedString attributedStringWithString:titleRow];
[attrTitle setFont:[UIFont systemFontOfSize:18] range:[titleRow rangeOfString:title]];
[attrTitle setFont:[UIFont systemFontOfSize:16] range:[titleRow rangeOfString:mtg_time]];

NSMutableAttributedString *attrLoc = [NSMutableAttributedString attributedStringWithString:loc];
NSMutableAttributedString *attrDetail = [NSMutableAttributedString attributedStringWithString:detail];
[attrDetail setFont:[UIFont systemFontOfSize:16]];

CGSize suggestedSize = [attrTitle sizeConstrainedToSize:CGSizeMake(300, FLT_MAX)];
CGRect frame1 = CGRectMake(10, 15, 300, suggestedSize.height);
OHAttributedLabel *lblTitle = [[OHAttributedLabel alloc] initWithFrame:frame1]; 
lblTitle.numberOfLines = 0;

CGSize size2 = [attrLoc sizeConstrainedToSize:CGSizeMake(300, FLT_MAX)];
CGRect frame2 = CGRectMake(10, suggestedSize.height+15+5, 300, size2.height);
OHAttributedLabel *lblLocation = [[OHAttributedLabel alloc] initWithFrame:frame2]; 
CGFloat temp = size2.height+suggestedSize.height;

CGSize size3 = [attrDetail sizeConstrainedToSize:CGSizeMake(300, FLT_MAX)];
CGRect frame3 = CGRectMake(10, temp+15+5, 300, size3.height);
OHAttributedLabel *lblDescription = [[OHAttributedLabel alloc] initWithFrame:frame3]; 
lblDescription.numberOfLines = 0;

lblTitle.attributedText = attrTitle;
lblLocation.attributedText = attrLoc;
lblDescription.attributedText = attrDetail;

for (NSTextCheckingResult *m in matches) {
    NSString *num = [detail substringWithRange:m.range];
    NSRange linkRange = [detail rangeOfString:num];
    [lblDescription addCustomLink:[NSURL URLWithString:@"user://certa"] inRange:linkRange];
}
lblDescription.delegate = self;

[self.view addSubview:lblTitle];
[self.view addSubview:lblDescription];
[self.view addSubview:lblLocation];


[lblTitle release];
[lblLocation release];
[lblDescription release];
}

1 个答案:

答案 0 :(得分:0)

每次OHAttributedLabel执行时,您都会在详细信息视图中添加三个viewDidAppear:的新实例。您永远不会删除这些视图。您需要在lblTitle中保存对它们的引用(lblDescriptionlblLocationMtgDetailController),并将其删除或在下次viewDidAppear:时重复使用它们执行。

MtgDetailController添加三个属性:

@property (retain) UILabel *lblTitle;
@property (retain) UILabel *lblDescription;
@property (retain) UILabel *lblLocation;

(别忘了@synthesize他们。)

viewDidAppear:的顶部,从其超级视图中删除标签:

- (void)viewDidAppear:(BOOL)animated {
    [self.lblTitle removeFromSuperview];
    [self.lblDescription removeFromSuperview];
    [self.lblLocation removeFromSuperview];
    ...

viewDidAppear:的底部,将新创建的标签保存在属性中:

self.lblTitle = lblTitle;
self.lblLocation = lblLocation;
self.lblDescription = lblDescription;