2015-11-28 73 views
0

我正在使用Objective-C。我想推一个视图控制器与表视图。当我使用普通的表格视图单元格时,效果很好。但是当我使用自定义单元格时,它无法加载并且模拟器冻结。无法加载与自定义单元格的表格

有没有什么问题可以导致模拟器冻结时推视图控制器?有人可以帮助我吗?

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

    NSString *simpleIdentifier = @"comment"; 
    commentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentifier]; 

    if (cell == nil){ 
     cell = [[commentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleIdentifier]; 
    } 

    cell.username.text = [comments[indexPath.row] objectForKey:@"user"]; 
    cell.textLabel.text = [comments[indexPath.row] objectForKey:@"content"]; 

    return cell; 

} 
+0

想不通是什么的话你post.So的问题,最好是张贴一些代码。 – Leo

+0

张贴代码.. –

+0

当你说模拟器被冻结时,程序是否崩溃?你能发布崩溃日志吗? – TheAppMentor

回答

1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *[email protected]"CustomCell"; 
    CustomCell *cell=(CustomCell*)[tableView dequeueReusableCellWithIdentifier:customTableIdentifier]; 
    if (cell==nil) 
    { 
    NSArray *nibs=[[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil]; 
    cell=[nibs objectAtIndex:0]; 
    } 
    cell.yourLabelData.text = @"iOS"; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row == 0) //If you want to go view controller from particular index to other View Controller 
    { 
     //Here write the code for push the view 
    } 
} 
+0

你的注册类别和标识符bro, –

+0

这里没有必要。我运行这个没有注册在viewDidLoad方法。 – user3182143

+0

我有xcode 7和iOS 9.在我的xcode中它已成功运行。 – user3182143

0
#pragma mark TableView delegete methods 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [Array count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *[email protected]"cellIdentifier"; 
    SocialTableViewCell *cell=(SocialTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SocialTableViewCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 
    //write code for display data 
    return cell; 
} 
0

您需要在viewDidLoad中

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    [tableView registerNib:[UINib nibWithNibName:@"myCustomCell" bundle:nil] forCellWithReuseIdentifier:@"myCell"]; 
} 
0

注册您的自定义单元格尝试这些方式..

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

    NSString *cellidentifier=[NSString stringWithFormat:@"%ld",(long)indexPath.row]; 
    CustomCell *cell= (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier]; 
    if (cell==nil) 
    { 
     cell=[[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier]; 
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 

    } 

    if (indexPath.row==0) 
    { 
     //Here change or edit specific custom cell if u want like i have UIImageview and i want to give it a specific image.. 
     [cell.imgView1 setImage:imgsp1];    
    } 
    return cell; 
} 

我希望它有助于..

0

在视图控制器

.H

@property (strong, nonatomic) IBOutlet UITableView *tableViewData; 

.M

#import "ViewController.h" 
#import "CustomCell.h" 

@interface ViewController() 
{ 
    NSDictionary *dictComments; 
    NSArray *arrayUser; 
    NSArray *arrayContents; 
} 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
    dictComments = [[NSDictionary alloc]initWithObjectsAndKeys:@"Jin",@"User",@"iOS Developer",@"Content", nil]; 
    arrayUser = [[NSArray alloc]initWithObjects:[dictComments objectForKey:@"User"], nil]; 
    arrayContents = [[NSArray alloc]initWithObjects:[dictComments objectForKey:@"Content"], nil]; 
} 


//UITableViewDataSource Methods 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return arrayUser.count; 
    //OR 
    return 1; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *[email protected]"CustomCell"; 
    CustomCell *cell=(CustomCell*)[tableView dequeueReusableCellWithIdentifier:customTableIdentifier]; 
    if (cell==nil) 
    { 
    NSArray *nibs=[[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil]; 
    cell=[nibs objectAtIndex:0]; 
    } 

    cell.labelUserName.text = [NSString stringWithFormat:@"%@",[arrayUser objectAtIndex:indexPath.row]]; 
    cell.labelTextContent.text = [NSString stringWithFormat:@"%@",[arrayContents objectAtIndex:indexPath.row]]; 

      //OR 

    cell.labelUserName.text = [NSString stringWithFormat:@"%@",[dictComments objectForKey:@"User"]]; 
    cell.labelTextContent.text = [NSString stringWithFormat:@"%@",[dictComments objectForKey:@"Content"]]; 

    return cell; 
} 
相关问题