IOS - IB:在 UIViewcontroller 中使用 UITableView,请帮助我

Posted

技术标签:

【中文标题】IOS - IB:在 UIViewcontroller 中使用 UITableView,请帮助我【英文标题】:IOS - IB : using UITableView in UIViewcontroller, please help me 【发布时间】:2012-08-21 14:20:53 【问题描述】:

我是新手 ios 并使用 IB,我在 UIViewController 中工作 UITableView,我在 viewcontroller 和 setdelegate 中实现了“UITableViewDelegate,UITableViewDataSource”,uitableview 的数据源,但它不起作用,我不知道,

请帮帮我!

感谢您阅读本文。

代码 viewcontroller.h

@interface ViewController : UIViewController<UITableViewDelegate,
UITableViewDataSource> @property (nonatomic, retain) IBOutlet UITableView *tableView;

代码 viewcontroller.m

- (void)viewDidLoad  [tableView setDataSource:self];
    [tableView setDelegate:self];
    dispatch_async(htvque, ^
               NSData* data = [NSData dataWithContentsOfURL: listFilmByCate];
        NSError* error;
        jsonTable = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
        listDataTable = [jsonTable objectForKey:@"List"];
                dispatch_async(dispatch_get_main_queue(), ^
                     [tableView reloadData];
        );
    );



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    return 1;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    return listDataTable.count;


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

    NSDictionary *dataTable = [listDataTable objectAtIndex:indexPath.row];  
    NSData *receivedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[dataTable objectForKey:@"Thumbnail"]objectForKey:@"Url1"]]];    UIImage *image = [[[UIImage alloc] initWithData:receivedData] stretchableImageWithLeftCapWidth:50 topCapHeight:80];

    static NSString *simple_cell = @"simpleCell";

    CustomizeCell_Home *customize_cell = (CustomizeCell_Home *)[tableView dequeueReusableCellWithIdentifier:simple_cell];
    if (simple_cell == nil) 
    

    customize_cell.imageView.image = image;
    customize_cell.lbldescription.text =[dataTable objectForKey:@"LongDescription"];
    customize_cell.lblTitle.text =  [dataTable objectForKey:@"VName"];
    customize_cell.lblTitle.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
    customize_cell.lbldescription.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
    customize_cell.lbldescription.numberOfLines=4;
    customize_cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgtblRight.png"]];
    return customize_cell;

【问题讨论】:

嗨 CBredlow,我想在 uiviewcontroller 中显示 uitableview?我为它设置了委托和数据源,但不起作用 【参考方案1】:

这行代码是你的问题:

CustomizeCell_Home *customize_cell = (CustomizeCell_Home *)[tableView dequeueReusableCellWithIdentifier:simple_cell];
if (simple_cell == nil) 

如果customize_cellnil 会发生什么?

你没有实例化customize_cell,因此你不能调用这些方法:

customize_cell.imageView.image = image;
customize_cell.lbldescription.text =[dataTable objectForKey:@"LongDescription"];
customize_cell.lblTitle.text =  [dataTable objectForKey:@"VName"];
customize_cell.lblTitle.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
customize_cell.lbldescription.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
customize_cell.lbldescription.numberOfLines=4;
customize_cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgtblRight.png"]];
return customize_cell;

只需在 if 中添加一个简单的初始化单元格,如下所示:

if (customize_cell == nil) 

  customize_cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"AnIdentifierString"] autorelease];

编辑

改变这个:

CustomizeCell_Home *customize_cell = (CustomizeCell_Home *)[tableView dequeueReusableCellWithIdentifier:simple_cell];
if (simple_cell == nil) 


customize_cell.lbldescription.text =[dataTable objectForKey:@"LongDescription"];
customize_cell.lblTitle.text =  [dataTable objectForKey:@"VName"];
customize_cell.lblTitle.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
customize_cell.lbldescription.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
customize_cell.lbldescription.numberOfLines=4;
customize_cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgtblRight.png"]];
return customize_cell;

到:

CustomizeCell_Home *customize_cell = (CustomizeCell_Home *)[tableView dequeueReusableCellWithIdentifier:simple_cell];
if (customize_cell == nil) 

  customize_cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"AnIdentifierString"] autorelease];


customize_cell.lbldescription.text =[dataTable objectForKey:@"LongDescription"];
customize_cell.lblTitle.text =  [dataTable objectForKey:@"VName"];
customize_cell.lblTitle.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
customize_cell.lbldescription.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
customize_cell.lbldescription.numberOfLines=4;
customize_cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgtblRight.png"]];
return customize_cell;

【讨论】:

嗨 nicos karalis,我像你一样声明,但 uitableview 仍然不显示数据 把它放在github上并在这里发布链接。这样更容易 只是为了确定,您是否 100% 确定您的 listDataTable 不为空?还是你加载的 json 真的被加载了? 这个链接是我的项目mediafire.com/download.php?4ji84io6mex6nlp希望尽快得到你的帮助 我查看了您的代码,但找不到任何问题。你检查过解析的json吗?我无法检查您的服务器是否提供了正确的答案,但请检查 NSData 是否不为零并在此行之后放置一个 NSLog listDataTable = [jsonTable objectForKey:@"List"]; 以查看该数组是否已填充【参考方案2】:

设置,

表视图。数据源 = 自我; 表视图。委托=自我;

实施 protocol 被指示为 @required 的意思是说强制

-(UITableViewCell*) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath 

    // dint concentrate on this part, hope u are right
    NSDictionary *dataTable = [listDataTable objectAtIndex:indexPath.row];  
    NSData *receivedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[dataTable objectForKey:@"Thumbnail"]objectForKey:@"Url1"]]];    
    UIImage *image = [[[UIImage alloc] initWithData:receivedData] stretchableImageWithLeftCapWidth:50 topCapHeight:80];

    static NSString *simple_cell = @"simpleCell";
    // works like double ended queue.... cells are re-used when available... so when cell is nil, u need to create
    CustomizeCell_Home *customize_cell = (CustomizeCell_Home *)[tableView dequeueReusableCellWithIdentifier:simple_cell];  
    if (simple_cell == nil) 
    
         // CustomizeCell_Home is linked to CustomizeCell_Home_Reference using nib,
         [[NSBundle mainBundle] loadNibNamed: @"CustomizeCell_Home" owner: self options: nil];

         customize_cell = CustomizeCell_Home_Reference;
    
    .....  
    .....  
    return customize_cell;

【讨论】:

嗨,Ashwin Kumar,我再次检查了代码,但我不知道为什么它不起作用并且没有显示错误:( 你能把代码贴出来吗……这样我就不会发现问题了……你是如何创建 CustomizeCell_Home 的 此链接我的代码mediafire.com/download.php?im0064q9m6kjie4,在 mocku.ps/2mhyhg 中查看 JSon,我设置了委托和数据源,但它不起作用...【参考方案3】:

首先,永远不会分配单元,而不是执行 if(simple_cell==nil),您应该执行 if(customize_cell==nil) 并在那里执行初始化代码。否则,您将比较刚刚创建的字符串并将值设置为与 nil。

【讨论】:

不想让你失望,但simple_cellstatic NSString 是的,我编辑了:customize_cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simple_cell];

以上是关于IOS - IB:在 UIViewcontroller 中使用 UITableView,请帮助我的主要内容,如果未能解决你的问题,请参考以下文章

iOS开发:IB VS 纯代码

iOS 自动布局:这可以用 IB 完成吗?

iOS:当其中一些可能改变大小时,如何通过IB正确调整UI元素的位置?

使用IB设计现有应用程序对设备没有影响(iOS 7.1 / XCode 5.1)

非法配置:不再支持编译早于 iOS 7 的 IB 文档

使用 IB / Xcode 4.5.1 和 iOS Simulator 6.0 自定义 UIView 操作方法