在UItableviewcell中创建标签

时间:2014-11-07 09:16:32

标签: ios uitableview

我想问一下,我想在Uitableview中创建标签programiticaly但是在从json加载数据后例如。我的数据库中有25条记录,我将按此返回

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

   // return 5;
}

和表格视图单元格填充如下:

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


}

我想添加一个名为“加载更多记录”的标签或按钮,我可以再次发送服务器调用..怎么做??? Thanx提前

2 个答案:

答案 0 :(得分:2)

首先,您的-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection实施应该返回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;
}

然后在-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath实现中,您可以检测最后一个单元格并以这种方式设置标签:

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

    }
}

然后您可以检测您的单元格是否以这种方式被点击:

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

          [tableView reloadData];
     }
}

答案 1 :(得分:0)

您可以使用以下行向您的单元格添加自定义按钮,例如:

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

修改

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

}