• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 在 uitableviewcell 中创建 UIlabel 的 Action

[复制链接]
菜鸟教程小白 发表于 2022-12-12 13:19:33 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在我的应用程序中制作一个收件箱模块。第一次加载时,有 20 条消息来自服务器。 我希望在 20 条消息之后,将在 UItableview 单元格中创建一个名为“加载更多消息”的标签。 单击该标签后,我想再次调用服务器。 我曾尝试以下代码,但它不工作。提前谢谢 It is showing like this i want to show it after recprds has been laoded 下面是我的 cellForRowAtIndexPath 和 numberOfRowsInSection 的示例代码

-(NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
{

    return [inboxmessagesarray count]+1;
}

-(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath{

    static NSString *tableviewidentifier = @"cell";
   __block  tablecellTableViewCell *cell= [self.activitiesTableView_ dequeueReusableCellWithIdentifier:tableviewidentifier];

    if(cell==nil)
    {
        cell = [[tablecellTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableviewidentifier];
    }if(indexPath.row == [self tableView:self.activitiesTableView_ numberOfRowsInSection:indexPath.section] - 1){
        cell.textLabel.text=@"Load More Record";// here i am making a label but it is not showing at the end of tableview cell



    }
    else{

     __block NSString *row = [NSString stringWithFormat"%ld",(long)indexPath.row];

    cell.titlename.font=[UIFont fontWithName"SegoeUI" size:15];
    cell.tolbl.font=[UIFont fontWithName"SegoeUI-light" size:12];
    cell.fromlbl.font=[UIFont fontWithName"SegoeUI-light" size:12];
    cell.datelbl.font=[UIFont fontWithName"SegoeUI-light" size:8];
    cell.timelbl.font=[UIFont fontWithName"SegoeUI-light" size:8];
      if([[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey"messageRead"] intValue]==0)
    {

        cell.titlename.font=[UIFont fontWithName"SegoeUI" size:15];
        cell.tolbl.font=[UIFont fontWithName"SegoeUI-light" size:12];
        cell.fromlbl.font=[UIFont fontWithName"SegoeUI-light" size:12];
        cell.datelbl.font=[UIFont fontWithName:@"SegoeUI-light" size:8];
        cell.timelbl.font=[UIFont fontWithName:@"SegoeUI-light" size:8];

                cell.contentView.backgroundColor=[UIColor lightGrayColor];



    }
    else
    {
        cell.titlename.font=[UIFont fontWithName:@"SegoeUI" size:15];
        cell.tolbl.font=[UIFont fontWithName:@"SegoeUI-light" size:12];
        cell.fromlbl.font=[UIFont fontWithName:@"SegoeUI-light" size:12];
        cell.datelbl.font=[UIFont fontWithName:@"SegoeUI-light" size:8];
        cell.timelbl.font=[UIFont fontWithName:@"SegoeUI-light" size:8];

        cell.contentView.backgroundColor=[UIColor clearColor];

    }
    cell.titlename.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"offerTitle"];
   //toCity

    cell.tolbl.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"toCity"];
    cell.fromlbl.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"fromCity"];
    if(![[imagesDictionary allKeys] containsObject:row]) //if image not found download and add it to dictionary
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
            NSString *img=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"offerPhoto"];// here i am getting image path
            NSURL *url = [NSURL URLWithString:img];
            NSData * imageData = [NSData dataWithContentsOfURL:url];
            UIImage *image = [UIImage imageWithData:imageData];

            dispatch_sync(dispatch_get_main_queue(), ^{ //in main thread update the image
                 [imagesDictionary setObject:image forKey:row];
                cell.profileimage.image = image;
                cell.textLabel.text = @""; //add this update will reflect the changes
                NSLog(@"loading and adding to dictionary");
            });
        });
    }
    else
    {
        cell.profileimage.image = [imagesDictionary objectForKey:row];
        NSLog(@"retriving from dictionary");
    }


    cell.datelbl.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"messageDate"];
    cell.timelbl.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"messageTime"];
    }

return cell;
}



Best Answer-推荐答案


由于您希望将一行添加到显示 Load More Records 的 tableview 中,您可以通过添加页脚 View 或每个最后一行,通过在方法 -(NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section 并在 -(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPath 中返回此行的特定单元格NSIndexPath *)indexPath

例如在表格 View 中添加一个自定义单元格并添加一个标签并将其文本设置为 Load more record 并将其重用标识符设置为 LabelCell

-(NSInteger)tableViewUITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return self.inboxmessagesarray.count + 1 ;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath  *)indexPath
{
   //[self performSegueWithIdentifier:@"segueIdentifier" sender:tableView];
   if(indexPath.row == [self.inboxmessagesarray count])
   {
      NSLog(@"load more records");
   }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  __block  tablecellTableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:tableviewidentifier];
  if(cell == nil)
  {
      cell = [[tablecellTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableviewidentifier];
  }
  //in the last row
  if(indexPath.row == self.inboxmessagesarray.count)
  {
      cell = [tableView dequeueReusableCellWithIdentifier:@"LabelCell"];
      if(cell == nil)
      {
          cell = [[tablecellTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LabelCell"];
      }
      if([self.inboxmessagesarray count] > 0)
          cell.hidden = NO;
      else
          cell.hidden = YES;

      return cell;
  }
  //..rest of the code 

关于ios - 在 uitableviewcell 中创建 UIlabel 的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27596953/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap