无法使用自定义单元格加载tableview

时间:2015-11-28 06:00:27

标签: ios objective-c uitableview tableviewcell

我正在使用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;

}

5 个答案:

答案 0 :(得分:1)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *customTableIdentifier=@"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
    }
}

答案 1 :(得分: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 *cellIdentifier=@"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;
}

答案 2 :(得分: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"];
}

答案 3 :(得分: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;
}

我希望它有所帮助......

答案 4 :(得分:0)

ViewController中的

·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 *customTableIdentifier=@"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;
 }