简体   繁体   中英

UITableViewCell textLabel attributedText iOS7

I am trying to set an attributedtext as the value for "textlabel" in a Basic UITableViewCell. However, the cell is shown as blank! Here is teh code

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell...
NSDictionary *docattr = [NSDictionary new];
NSError *myerror = [NSError new];
NSString *htmlString = [[[dm latestUpdates] objectAtIndex:indexPath.row] objectForKey:@"display_data"];
NSMutableAttributedString *updateText = 
[[NSMutableAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] 
                                        options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, 
                                                  NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} 
                             documentAttributes:&docattr 
                                          error:&myerror];

[cell.textLabel setAttributedText:updateText];
//   cell.detailTextLabel.attributedText = updateText;
NSLog(@"cell text %@", cell.textLabel.text);
//   cell.textLabel.text = htmlString;
return cell;

The above code translates my html to a valid attributed string. but nothing is shown. the html in question is

<div class='title'><a href='mc://guru_txn/171'>Wilbur Ross bought EXCO Resources, Inc.</a></div><div class='description'>Wilbur Ross bought 19,599,973 shares of EXCO Resources, Inc. (<a href='mc://quotes/XCO'>XCO</a>) @ $5.00 per share on 2014-01-17.</div>

I donot know how to specify the values i want various "class"es. However, the html fragment above is parsed into a valid attributed string.

I have tested your code, there seems to be something wrong with your html string.

I tried setting htmlString to @" <div>Test</div> " and it worked. However, when I wrapped "test" in tag, it wouldn't be visible anymore.

Hope this will help you to solve it.

PS: Also, try not to allocate objects in tableView:cellForRowAtIndexPath : method. It can really slow down your scrolling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM