简体   繁体   中英

Creating label in UItableviewcell

I want to ask that i want to create label programiticaly in Uitableview but after loading data from json For example. I have 25 records in my data base which i am returning like this

  -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[[[FBModelManager sharedModelManager] getModelDictionary:kModelTransporterActivities] objectForKey:[NSString stringWithFormat:@"%d",[[FBUserManager sharedUserManager] userId]]] count];

   // return 5;
}

and table view cell is populated like this :

  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger x=[[[[FBModelManager sharedModelManager] getModelDictionary:kModelTransporterActivities] objectForKey:[NSString stringWithFormat:@"%d",[[FBUserManager sharedUserManager] userId]]] count];

    static NSString *tableviewidentifier = @"InboxIdentifier";
    UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:tableviewidentifier];
    if(cell==nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableviewidentifier];
//        UIButton * newAction = [UIButton buttonWithType:UIButtonTypeCustom];
//        newAction.frame = CGRectMake(250, 10, 30, 30);
//        [newAction addTarget:self action:@selector(loadMoreRecords:) forControlEvents:UIControlEventTouchUpInside];
//        [cell.contentView addSubview:newAction];



    }
     FBTransporterActivityModel *activityModel = [[[[FBModelManager sharedModelManager] getModelDictionary:kModelTransporterActivities] objectForKey:[NSString stringWithFormat:@"%d",[[FBUserManager sharedUserManager] userId]]] objectAtIndex:(int)indexPath.row];

    if(activityModel)
    {
        [cell.textLabel setText:activityModel.userName];


    }
    else
        [cell.textLabel setText:@"saba"];


    if(indexPath.row == [self tableView:tableView numberOfRowsInSection:indexPath.section] - 1){
        [[cell textLabel] setText:@"Load more records"];
    }

   // cell.textLabel.text=@"saba";



    return cell;


}

i want to add a label or button named "Load more records" on which i can again send a server call.. how it can done??? Thanx in advance

First your -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection implementation should return count + 1

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //perhaps should be cached for more performace
    return [[[[FBModelManager sharedModelManager] getModelDictionary:kModelTransporterActivities] objectForKey:[NSString stringWithFormat:@"%d",[[FBUserManager sharedUserManager] userId]]] count] + 1;
}

Then in your -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath implementation you can detect the last cell and set a label this way:

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

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

    if(indexPath.row == [self tableView:tableView numberOfRowsInSection:indexPath.section] - 1){
         [[cell textLabel] setText:@"Load more records"];
    } else {

        FBTransporterActivityModel *activityModel = [[[[FBModelManager sharedModelManager] getModelDictionary:kModelTransporterActivities] objectForKey:[NSString stringWithFormat:@"%d",[[FBUserManager sharedUserManager] userId]]] objectAtIndex:(int)indexPath.row];
        if(activityModel){
            [cell.textLabel setText:activityModel.userName];
        }

    }
}

Then you can detect if your cell was clicked this way:

- (void) tableView:(UITableView*) tableView didSelecrRoqAtIndexPath:(IndexPath*) indexPath {
     if(indexPath.row == [self tableView:tableView numberOfRowsInSection:indexPath.section] - 1){
          // load your data and then:

          [tableView reloadData];
     }
}

You can use the following lines to add a custom button to your cell for example :

UIButton * newAction = [UIButton buttonWithType:UIButtonTypeCustom];
newAction.frame = CGRectMake(250, 10, 30, 30);
[newAction addTarget:self action:@selector(loadMoreRecords:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:newAction];

EDIT

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *tableviewidentifier = @"InboxIdentifier";
    UITableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:tableviewidentifier];
    if(cell==nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableviewidentifier];

         UIButton * newAction = [UIButton buttonWithType:UIButtonTypeCustom];
        newAction.frame = CGRectMake(250, 10, 30, 30);
        [newAction addTarget:self action:@selector(loadMoreRecords:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:newAction];
    }
     FBTransporterActivityModel *activityModel = [[[[FBModelManager sharedModelManager] getModelDictionary:kModelTransporterActivities] objectForKey:[NSString stringWithFormat:@"%d",[[FBUserManager sharedUserManager] userId]]] objectAtIndex:(int)indexPath.row];
    if(activityModel)
    {
        [cell.textLabel setText:activityModel.userName];
    }

    return cell;
}

- (IBAction) loadMoreRecords:(id)sender {

  // Make your request your server 

  // Update your [FBModelManager sharedModelManager]

  // reload your tableview
  [self.tableView reloadData];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM