IOS-IB:在UIViewcontroller中使用UITableView,请帮助我
我是IOS新手,使用IB,我在UIViewController中使用UITableView,我在viewcontroller和setdelegate中实现了“UITableViewDelegate,UITableViewDataSource”,但它不工作,我不知道 请帮帮我 感谢您阅读本文 代码viewcontroller.hIOS-IB:在UIViewcontroller中使用UITableView,请帮助我,ios,json,uitableview,uiviewcontroller,storyboard,Ios,Json,Uitableview,Uiviewcontroller,Storyboard,我是IOS新手,使用IB,我在UIViewController中使用UITableView,我在viewcontroller和setdelegate中实现了“UITableViewDelegate,UITableViewDataSource”,但它不工作,我不知道 请帮帮我 感谢您阅读本文 代码viewcontroller.h @interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataS
@interface ViewController : UIViewController<UITableViewDelegate,
UITableViewDataSource> @property (nonatomic, retain) IBOutlet UITableView *tableView;
首先,从不分配单元格,而不是执行if(simple_cell==nil),您应该执行if(customize_cell==nil)并在那里执行初始化代码。否则,您将比较刚创建的字符串并将值设置为nil。这行代码中的问题是:
CustomizeCell_Home *customize_cell = (CustomizeCell_Home *)[tableView dequeueReusableCellWithIdentifier:simple_cell];
if (simple_cell == nil)
{}
如果customize_cell
为nil
,会发生什么情况
您没有实例化customize\u单元格
,因此无法调用以下方法:
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;
致:
设定
桌面视图。数据源=自我桌面视图。代表=自我 实施协议,该协议被指定为@必需的意思是说必需的
-(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;
}hi CBredlow,我想在uiviewcontroller中显示uitableview?我为它设置了Delegate和datasource,但不想让您失望,但是
simple_cell
是一个静态NSString
是的,我编辑了这样的内容:customize_cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simple_cell];你好,nicos karalis,我声明我和你一样,但是uitableview仍然没有显示数据。请将它放到github上,并在这里发布链接。这种方法更简单只是为了确定,您是否100%确定您的listDataTable
不是空的?或者你加载的json真的被加载了?这个链接是我的项目,希望能得到你的帮助,因为我已经看过你的代码了,我没有发现任何问题。你检查过被解析的json了吗?我无法检查您的服务器是否提供了正确的answare,但请检查NSData是否为nil,并将NSLog放在这一行的后面listDataTable=[jsonTable objectForKey:@“List”]
为了查看这个数组是否被填充Hi,Ashwin Kumar,我再次检查了代码,我不知道为什么它不起作用&没有显示错误:(你可以发布代码…这样我就不必担心问题了…你是如何创建自定义cell_home这个链接我的代码,mocku.ps/2mhyhg中的JSon视图,我设置了委托和数据源,但它不起作用。。。
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;
-(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;