PAn Kaj Khatri
PAn Kaj Khatri

Reputation: 539

TableView not showing label ios

i am adding a table in NSObject calls so i can create the custom view. But tableView cellForRowAtIndexPath is being called but not label is shown. i have taken a view and adding the table on that view and added this view in the super class.

The code is as below:

- (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;

}

Upvotes: 1

Views: 108

Answers (2)

Amrit Sidhu
Amrit Sidhu

Reputation: 1950

Just edit the 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;
}

Upvotes: 1

user3182143
user3182143

Reputation: 9589

In 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;
}

Upvotes: 1

Related Questions