gpt4 book ai didi

ios - 在 UItableviewcell 中创建标签

转载 作者:行者123 更新时间:2023-11-28 21:55:21 24 4
gpt4 key购买 nike

我想问我想在 Uitableview 中以编程方式创建标签,但是在从 json 例如加载数据之后。我的数据库中有 25 条记录,我将像这样返回

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

// return 5;
}

表格 View 单元格是这样填充的:

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


}

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

最佳答案

首先你的-(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];
}
}

关于ios - 在 UItableviewcell 中创建标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26797820/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com