2015-11-28 98 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; 
} 
相關問題