2015-11-02 256 views
1

我在NSObject调用中添加一个表,所以我可以创建自定义视图。 但是tableView cellForRowAtIndexPath正在被调用,但没有显示标签。 我已经采取了一种观点,并在该视图上添加了表格,并在超类中添加了该视图。TableView不显示标签ios

的代码如下:

- (id)initWithFrame:(CGRect)frame 
{ 
self = [super init]; //calls init because UIResponder has no custom init methods 
if (self){ 
    [self allocViewWithFrame:frame]; 
} 
return self; 
} 

-(void)allocViewWithFrame:(CGRect)frame{ 
view = [[UIView alloc]initWithFrame:frame]; 

TableViewChat =[[UITableView alloc]initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height)]; 
TableViewChat.delegate = self; 
TableViewChat.dataSource = self; 
[view addSubview:TableViewChat]; 
dispatch_async(dispatch_get_main_queue(), ^{ 
    [TableViewChat reloadData]; 
}); 

} 

#pragma mark- tableView delegate and data source 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
// Return the number of sections. 
return 1; 
} 
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
return 144.0f; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
//#warning Incomplete implementation, return the number of rows 
return 5; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SimpleTableCell"]; 

if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:@"SimpleTableCell"]; 

} 

cell.textLabel.text = @"Cell"; 
return cell; 

} 
+0

您可以使用 “dequeueReusableCellWithIdentifier在indexpath:” – engmahsa

回答

1

只需编辑的cellForRowAtIndexPath

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


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdntifier"]; 
if(cell == nil) 
{ 
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCellIdentifier]; 
} 

cell.titleLabel.text = @"First" 

return cell; 
} 
1

在的cellForRowAtIndexPath

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

    static NSString *strCellIdentifier = @"CellIdntifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCellIdentifier]; 
    if(cell == nil) 
    { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCellIdentifier]; 
    } 

    cell.titleLabel.text = @"First" 

    return cell; 
}