Objective c 数据未添加到单元格
我已经在服务器上设置了一个断点Objective c 数据未添加到单元格,objective-c,Objective C,我已经在服务器上设置了一个断点 Cateogry_Cell * cell = (Cateogry_Cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 发现它总是返回零,我不知道为什么它会通过 if(cell == nil) { cell = [[Cateogry_Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:
Cateogry_Cell * cell = (Cateogry_Cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
发现它总是返回零,我不知道为什么它会通过
if(cell == nil) {
cell = [[Cateogry_Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cateogry_Cell"];
}
要么是他们的问题,我看不到,要么我的代码在零部分是错误的,但我不是100%的放在它无论如何
我使用的是故事板,所以我在其中创建了我的单元格并设置了样式,而不是通过代码
#import "Category_TableView.h"
#import "Cateogry_Cell.h"
#import "JobList_Table.h"
#import "JobList_Cell.h"
#import <QuartzCore/QuartzCore.h>
@interface Category_TableView ()
@end
@implementation Category_TableView
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.hidden = YES;
// Do any additional setup after loading the view.
[self.Category_Table registerClass:[Cateogry_Cell class] forCellReuseIdentifier:@"Cateogry_Cell"];
NSString *requestURL = [NSString stringWithFormat:@"http://URL.4fx.co.uk/forms/AppForms/app_latest.ashx"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: requestURL]];
NSURLConnection *con =[[NSURLConnection alloc] initWithRequest:request delegate:self];
if(con)
{
self.CatData = [[NSMutableData alloc] init];
self.Category_Array = [[NSMutableArray alloc] init];
}
else
{
[[[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to get connection. Please try again later" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[self.CatData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.CatData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[[[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to get connection. Please try again later" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *temp = [[NSString alloc] initWithData:self.CatData encoding:NSUTF8StringEncoding];
NSArray *jobs = [temp componentsSeparatedByString:@"||||"];
NSMutableDictionary *temp_dict = [[NSMutableDictionary alloc] init];
NSArray *jobInfo = [[NSArray alloc] init];
int count = 0;
for(int i = 1; i < [jobs count]; i++)
{
jobInfo = [[jobs objectAtIndex:i] componentsSeparatedByString:@"||"];
@try {
temp_dict = [[NSMutableDictionary alloc]init];
[temp_dict setObject:[jobInfo objectAtIndex:0] forKey:@"Category"];
[self.Category_Array addObject:temp_dict];
// NSDictionary *tempfile = [self.Category_Array objectAtIndex:i - 1];
// [[[UIAlertView alloc] initWithTitle:@"Added to Cat" message:[tempfile objectForKey:@"Category"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
count++;
}
@catch (NSException *exception) {
[[[UIAlertView alloc] initWithTitle:@"Exception Thrown" message:exception.reason delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
}
}
[_Category_Table reloadData];
}
//#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.Category_Array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cateogry_Cell";
Cateogry_Cell * cell = (Cateogry_Cell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil) {
cell = [[Cateogry_Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSDictionary *temp = [self.Category_Array objectAtIndex:[indexPath row]];
cell.Category_Cell_Label.text = [temp objectForKey:@"Category"];
//[[[UIAlertView alloc] initWithTitle:@"In Table Function" message:[temp objectForKey:@"Category"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
return cell;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"Cat_Cell_Push"])
{
JobList_Table *DetailController = [segue destinationViewController];
NSIndexPath *MyIndexPath = [_Category_Table indexPathForSelectedRow];
int row = [MyIndexPath row];
DetailController.JobList_Array = @[_Category_Array[row]];
}
}
@end
#导入“Category_TableView.h”
#导入“cateogray_Cell.h”
#导入“作业列表\表h”
#导入“作业列表\单元格.h”
#进口
@接口类别\u表视图()
@结束
@实现类别\u表视图
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}
-(无效)viewDidLoad
{
[超级视图下载];
self.navigationController.navigationBar.hidden=是;
//加载视图后执行任何其他设置。
[self.Category_表注册表类:[Category_Cell类]forCellReuseIdentifier:@“Category_Cell”];
NSString*requestURL=[NSString stringWithFormat:@]http://URL.4fx.co.uk/forms/AppForms/app_latest.ashx"];
NSURLRequest*request=[NSURLRequest requestWithURL:[NSURL URLWithString:requestURL]];
NSURLConnection*con=[[NSURLConnection alloc]initWithRequest:request委托:self];
如果(con)
{
self.CatData=[[NSMutableData alloc]init];
self.Category_Array=[[NSMutableArray alloc]init];
}
其他的
{
[[[UIAlertView alloc]initWithTitle:@“错误”消息:@“无法获取连接。请稍后重试”委托:nil cancelButtontile:@“确定”其他Buttontiles:nil]show];
}
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(void)连接:(NSURLConnection*)连接DidReceiverResponse:(NSURResponse*)响应
{
[self.CatData setLength:0];
}
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据
{
[self.CatData-appendData:data];
}
-(无效)连接:(NSURLConnection*)连接失败错误:(NSError*)错误
{
[[[UIAlertView alloc]initWithTitle:@“错误”消息:@“无法获取连接。请稍后重试”委托:nil cancelButtontile:@“确定”其他Buttontiles:nil]show];
}
-(无效)连接IDFinishLoading:(NSURLConnection*)连接
{
NSString*temp=[[NSString alloc]initWithData:self.CatData编码:NSUTF8StringEncoding];
NSArray*作业=[临时组件由字符串分隔:@“| | | |”];
NSMutableDictionary*temp_dict=[[NSMutableDictionary alloc]init];
NSArray*jobInfo=[[NSArray alloc]init];
整数计数=0;
对于(int i=1;i<[作业计数];i++)
{
jobInfo=[[jobs objectAtIndex:i]组件由字符串分隔:@“| |”];
@试一试{
temp_dict=[[NSMutableDictionary alloc]init];
[temp_dict setObject:[jobInfo objectAtIndex:0]forKey:@“Category”];
[self.Category_Array addObject:temp_dict];
//NSDictionary*tempfile=[self.Category_数组objectAtIndex:i-1];
//[[UIAlertView alloc]initWithTitle:@“添加到Cat”消息:[tempfile objectForKey:@“类别”]委托:无取消按钮:@“确定”其他按钮:无]显示];
计数++;
}
@捕获(NSException*异常){
[[[UIAlertView alloc]initWithTitle:@“引发异常”消息:Exception.reason委托:nil CancelButtonTile:@“确定”其他ButtonTiles:nil]show];
}
}
[_Category_Table reloadData];
}
//#pragma标记-表视图数据源
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
//返回节数。
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
//返回节中的行数。
返回[self.Category_数组计数];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*cellIdentifier=@“Cateogray_Cell”;
Categry_Cell*Cell=(Categry_Cell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
如果(单元格==nil){
cell=[[cateogray\u cell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:cellIdentifier];
}
NSDictionary*temp=[self.Category_数组objectAtIndex:[indexPath行]];
cell.Category_cell_Label.text=[temp objectForKey:@“Category”];
//[[[UIAlertView alloc]initWithTitle:@“表中函数”消息:[temp objectForKey:@“类别”]委托:无取消按钮:@“确定”其他按钮:无]显示];
返回单元;
}
-(void)prepareForSegue:(UIStoryboardSegue*)segue发送方:(id)发送方
{
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cateogry_Cell";
Cateogry_Cell * cell = (Cateogry_Cell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil) {
cell = [[Cateogry_Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSDictionary *temp = [self.Category_Array objectAtIndex:[indexPath row]];
cell.Category_Cell_Label.text = [temp objectForKey:@"Category"];
//[[[UIAlertView alloc] initWithTitle:@"In Table Function" message:[temp objectForKey:@"Category"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
return cell;
}
[self.tableView registerClass:[Category_Cell class] forCellReuseIdentifier:@"Cateogry_Cell"];