Search code examples
iphoneobjective-cxcodeuitableviewnsarray

how to show objects of nsarray on lable on tableview?


i have an NSArray of friends list on viewdidload , then i have a customized table view each cell consists a Lable, i want to show array elements in Lable on table view cell .

my code as

tableList1 = [[NSArray 

alloc]initWithObjects:@"Harendra",@"Satyendra",@"Jitendra",@"Sandeep",@"Dick",@"nihyan",@"alex

",@"Gorav",nil];

and

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];


    }


//         ********   LableView    *******


UILabel* cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(200,12, 141, 111)];
cellLabel.textColor=[UIColor greenColor];
cellLabel.font=[UIFont boldSystemFontOfSize:18];
//cell.textLabel.text = [callDetailArray objectAtIndex:indexPath.row];
cell.textLabel.text =[tableList1 objectAtIndex:indexPath.row];
//  cellLabel.text =[NSArray arrayWithArray:tableList];
cellLabel.text = @"Lorem Ipsum";
cellLabel.backgroundColor = [UIColor clearColor];
cellLabel.opaque = NO;

[cell.contentView addSubview:cellLabel];



 NSLog(@" arr count in Friends list   %i",tableList1.count);

Solution

  • Use this code

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    
    //********   LableView    *******
    
    UILabel* cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,12, 141, 111)];
    cellLabel.textColor=[UIColor greenColor];
    cellLabel.font=[UIFont boldSystemFontOfSize:18];
    cellLabel.text = [tableList1 objectAtIndex:indexPath.row];
    cellLabel.backgroundColor = [UIColor clearColor];
    cellLabel.opaque = NO;
    
    [cell.contentView addSubview:cellLabel];
    

    OR check this two method you added in your code.

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [tableList1 count];
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 50;
    }
    

    I hope this code useful for you.