0

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

请帮我!

感谢您阅读这篇文章。

代码 viewcontroller.h

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

代码视图控制器.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;
}
4

3 回答 3

1

在这行代码中是你的问题:

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

如果customize_cell是会发生什么nil

你没有实例化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;
于 2012-08-21T14:28:59.137 回答
0

放,

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

实现被定向为@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;

}

于 2012-08-21T14:44:39.960 回答
-1

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

于 2012-08-21T14:25:54.363 回答