请帮助我!
感谢您阅读本文。
code viewcontroller.h
@interface ViewController : UIViewController<UITableViewDelegate,
UITableViewDataSource> @property (nonatomic, retain) IBOutlet UITableView *tableView;
code 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;
}
答案 0 :(得分: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中添加一个简单的inicialize单元格,如下所示:
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;
答案 1 :(得分:0)
设置,
的tableView。 dataSource = self;
的tableView。 delegate = self;
实施协议,其定向为 @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;
}
答案 2 :(得分:-1)
首先,从不分配单元格,而不是if(simple_cell == nil),你应该做if(customize_cell == nil)并在那里做初始化代码。否则,您将比较刚创建的字符串并将值设置为nil。