ios - 在uitableviewcell中创建UIlabel的action

标签 ios objective-c uitableview

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [inboxmessagesarray count]+1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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;
}

最佳答案

由于您希望将一行添加到显示加载更多记录的表格 View 中,因此您可以通过两种方式来完成此操作:添加页脚 View 或每最后一行,通过在方法 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 并返回 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath 中该行的特定单元格:(NSIndexPath *)indexPath

例如,在表格 View 中再添加一个自定义单元格,添加一个标签,并将其文本设置为加载更多记录,并将其重用标识符设置为LabelCell

-(NSInteger)tableView:(UITableView *)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/

相关文章:

ios - 有什么办法可以将 Xcode 项目从 8 降级到 7?

ios - 从应用程序委托(delegate)设置标签栏角标(Badge)

ios - 如何从 UITableView 中获取选定的行?

ios - 单击表格行时在 SecondViewController 上显示错误信息

iphone - 为什么我在此代码中得到 "unknown type name NSManagedObjectContext"?

ios - 如何从另一个 View Controller 将数据插入到 tableView 中?

objective-c - 将 UIButton 放入 UITableView 中

ios - 使用 CCClippingNode 的屏幕截图 - cocos2d-iphone-2.1-beta4

ios - 单击 UIImageView 更改 ViewController

ios - Xcode:左上角和右上角半径不起作用