2012-11-26 83 views
0

我在創建的自定義UITableViewCell中有這個UIWebView在自定義的UITableViewCell上實現UIWebView

問題是,當我在"MyMain"中實現UIWebView時,它正在工作,但是當我滾動文本時,會一遍又一遍在單元格上讀取文本。

當我在自定義單元類中實現UIWebView時,它根本沒有顯示它。

UITableViewCustomCell.h

@property (nonatomic, weak) IBOutlet UIWebView *wvMainText; 
@property (nonatomic, strong) NSString *wvTitle; 
@property (nonatomic, strong) NSString *wvPrice; 

UITableViewCustomCell.m

@synthesize wvMainText = _wvMainText; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) 
    { 
     // THIS IS WHERE I IMPLEMENT THE WEBVIEW? 
    } 
    return self; 
} 

MyMain

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"myListCell"; 

    UITableViewCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"UITableViewCustomCell" owner:nil options:nil]; 

     for (UIView *view in views) { 
      if([view isKindOfClass:[UITableViewCell class]]) 
      { 
       cell = (UITableViewCustomCell *)view; 
      } 
     } 
    } 

    cell.wvTitle = [[self.arrList objectAtIndex:indexPath.row] valueForKey:@"title"]; 
    cell.wvPrice = [[self.arrList objectAtIndex:indexPath.row] valueForKey:@"price"]; 

    wv.scrollView.scrollEnabled = NO; 

    NSString *htmlString = [NSString stringWithFormat: 
         @"<html><body>%@,%@</body></html>", self.wvTitle, self.wvPrice]; 

    [wv loadHTMLString:htmlString baseURL:nil]; 
    [wv setBackgroundColor:[UIColor clearColor]]; 
    [wv setOpaque:NO]; 

    return cell; 
} 

回答

1
Ť

這個......它可以解決你的問題。 在你的cellForRowAtIndexPath方法,請做這樣的變化....

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath 
{ 
static NSString *CellIdentifier = @"myListCell"; 

UITableViewCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 

    NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"UITableViewCustomCell" owner:nil options:nil]; 

    for (UIView *view in views) { 
     if([view isKindOfClass:[UITableViewCell class]]) 
     { 
      cell = (UITableViewCustomCell *)view; 
     } 
    } 
     cell.wvTitle = [[self.arrList objectAtIndex:indexPath.row] valueForKey:@"title"]; 
    cell.wvPrice = [[self.arrList objectAtIndex:indexPath.row] valueForKey:@"price"]; 

    wv.scrollView.scrollEnabled = NO; 

    NSString *htmlString = [NSString stringWithFormat: 
        @"<html><body>%@,%@</body></html>", self.wvTitle, self.wvPrice]; 

    [wv loadHTMLString:htmlString baseURL:nil]; 
    [wv setBackgroundColor:[UIColor clearColor]]; 
    [wv setOpaque:NO]; 

} 


    return cell; 
} 
+0

你做了哪些改變?請向我解釋.. –

+0

我已經把webview代碼放在if codition裏面了,所以它只會爲一個單元添加一次......它工作與否? – Murali

+0

像魅力男人一樣工作!非常感謝你! –