UIWebView在自定义TableView单元格中

时间:2012-07-12 20:40:14

标签: iphone objective-c ios cocoa-touch uitableview

我有一个简单的Interface Builder文件,它是一个Custom TableView Cell。自定义tableview单元有两个出口......

  1. web视图
  2. 文字标签
  3. 出于某种奇怪的原因,我收到了这个错误...

    2012-07-12 16:28:23.206 VideoPush[3761:707] -[UITableViewCell webView]: unrecognized selector sent to instance 0x168cc0
    2012-07-12 16:28:23.211 VideoPush[3761:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell webView]: unrecognized selector sent to instance 0x168cc0'
    *** First throw call stack:
    (0x355e188f 0x37988259 0x355e4a9b 0x355e3915 0x3553e650 0x87279 0x33075efb 0x33074fd9 0x33074763 0x33018f37 0x355401fb 0x32410aa5 0x324106bd 0x32414843 0x3241457f 0x3243c911 0x3243c8e3 0x3305a10f 0x33047b33 0x33015ac3 0x33015567 0x33014f3b 0x371d422b 0x355b5523 0x355b54c5 0x355b4313 0x355374a5 0x3553736d 0x3304686b 0x33043cd5 0x86e5b 0x86e00)
    terminate called throwing an exception(lldb) 
    

    我的XIB

    enter image description here

    这里是 CustomCell.h 上面xib的控制器......

    #import <UIKit/UIKit.h>
    
    @interface VPCustomCell : UITableViewCell
    @property (strong, nonatomic) IBOutlet UIWebView *webView;
    @property (strong, nonatomic) IBOutlet UILabel *titleLabel;
    
    @end
    

    最后是我的视图控制器,它包含实际的表视图...

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        NSString *htmlString = @"<html><head> <meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 120\"/></head> <body style=\"background:#FFF;margin-top:0px;margin-left:0px\"> <div><object width=\"120\" height=\"80\"> <param name=\"movie\" value=\"http://www.youtube.com/v/1Xqn5IHbusA&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"></param> <param name=\"wmode\" value=\"transparent\"></param> <embed src=\"http://www.youtube.com/v/1Xqn5IHbusA&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"120\" height=\"80\"></embed> </object></div></body></html>";
        // Configure the cell...
        [cell.webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:@"http://www.youtube.com"]];
        cell.titleLabel.text = @"Hello";
    
        return cell;
    }
    

    注意:在接口文件中我声明了这个并导入我的自定义单元格...

    VPCustomCell *cell;
    

    我理解的是错误是什么问题?

2 个答案:

答案 0 :(得分:0)

在Interface Builder中,查看Identity Inspector(option-command-3),并确保已将表的类别设置为CustomCell单元格类。

答案 1 :(得分:-3)

使用UITableViewCells时,最好处理标签而不是插座。对于您的问题,我会删除出口,在属性检查器中给Webview一个标签或者你想要的任何东西,比如100.为标签做同样的事情。然后在你的cellForRowAtIndexPath:

static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

NSString *htmlString = @"<html><head> <meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 120\"/></head> <body style=\"background:#FFF;margin-top:0px;margin-left:0px\"> <div><object width=\"120\" height=\"80\"> <param name=\"movie\" value=\"http://www.youtube.com/v/1Xqn5IHbusA&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"></param> <param name=\"wmode\" value=\"transparent\"></param> <embed src=\"http://www.youtube.com/v/1Xqn5IHbusA&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"120\" height=\"80\"></embed> </object></div></body></html>";
// Configure the cell...
UIWebView *thisWebView = (UIWebView*)[cell viewWithTag:100];
UILabel *thisLabel = (UILabel*)[cell viewWithTag:whateverYouWant];
[thisWebView loadHTMLString:htmlString baseURL:[NSURL URLWithString:@"http://www.youtube.com"]];
thisLabel.text = @"Hello";
return cell;

你应该很好

相关问题