iphoneobjective-csdkttstyledtextlabel

Display A TTStyledTextLabel in a UITableView


How can I set a TTStyledTextLabel inside of a UITableView. Each TTStyledTextLabel contains Some parsed HTML.

Heres what I have I realize its probably completely wrong.

TTStyledTextLabel* label = [[TTStyledTextLabel alloc] autorelease];
cell.textLabel.text = [TTStyledText textFromXHTML:tempString lineBreaks:YES URLs:YES];

App Crashes on launch. I think its because I am setting the .text property with something that is not text. However, I don't know what else to set.


Solution

  • The following code will do what you want. Unfortunately, however, I cannot figure out how to automatically set the height. If memory isn't an issue you could keep a seperate array of TTStyledTextLabels and reference their heights.

    in your loadView:

    CGRect cgRct2 = CGRectMake(0, 35, 320, 375); //define size and position of view 
        tblView = [[UITableView alloc] initWithFrame:cgRct2 style:UITableViewStylePlain];
        tblView.dataSource = [self constructDataSource];
        tblView.delegate = self;
        //[tblView reloadData];
        [myView addSubview:tblView];
    

    in your class:

    -(TTListDataSource *)constructDataSource {
        NSLog(@"constructDataSource");
        NSMutableArray * namesArray = [[NSMutableArray alloc] init];
    
        //ADD ITEMS
        [namesArray addObject:[TTStyledText textFromXHTML:[NSString stringWithString:@"some XHTML"]]];
    
    
    
    
        TTListDataSource * dataSource = [[TTListDataSource alloc] init];
        for (int i = 0; i < [namesArray count]; i++) {
            TTStyledText * text = [namesArray objectAtIndex:i];
    
            [dataSource.items addObject:[TTTableStyledTextItem itemWithText:text]];
        }
    
        [namesArray release];
        return dataSource;
    }