Ios 在tableviewcell中创建UI标签的操作

Ios 在tableviewcell中创建UI标签的操作,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在我的应用程序中制作一个收件箱模块。在第一次加载时,有20条消息来自服务器。 我希望在20条消息之后,在UItableview单元格中创建一个名为“加载更多消息”的标签。 点击标签后,我想再打一次电话给服务器。 我尝试了以下代码,但不起作用。thanx提前:( 下面是我的cellForRowAtIndexPath和numberOfRowsInSection的示例代码 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSe

我正在我的应用程序中制作一个收件箱模块。在第一次加载时,有20条消息来自服务器。 我希望在20条消息之后,在UItableview单元格中创建一个名为“加载更多消息”的标签。 点击标签后,我想再打一次电话给服务器。 我尝试了以下代码,但不起作用。thanx提前:( 下面是我的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;
}

将标签添加为表的页脚视图

tableview.tableFooterView = label;
这样,您的标签将始终位于桌子的末尾


要加载更多动作,请在标签上添加点击手势。

尝试使用
UITableView的
方法

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

    UIView * viewHeader = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
    [viewHeader setBackgroundColor:[UIColor redColor]];

    UIButton * btnLoadMore = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnLoadMore setFrame:CGRectMake(10, 5, 300, 30)];
    [btnLoadMore setTitle:@"Load More Record" forState:UIControlStateNormal];
    [btnLoadMore setTintColor:[UIColor blackColor]];
    [btnLoadMore setBackgroundColor:[UIColor clearColor]];
    [btnLoadMore addTarget:self action:@selector(loadMoreRecords:) forControlEvents:UIControlEventTouchUpInside];
    [viewHeader addSubview:btnLoadMore];
    return viewHeader;
}

- (void)loadMoreRecords : (id) sender {
    NSLog(@"Your code to load more data");
}  

如果您只想在tableView的底部或下方显示某些内容,只需执行一个快速步骤:

这将免除UILabels框架或原点的任何自动布局问题(它永远不会移动)


当您希望将显示
加载更多记录的行添加到tableview时
您可以通过两种方式来完成,一种是添加页脚视图,另一种是通过在方法
-(NSInteger)tableview:(UITableView*)中传递一个额外的行来完成节,并在
-(UITableViewCell*)tableView:(UITableView*)tableView cell for rowatinexpath:(nsindepath*)indepath中返回此行的特定单元格

例如,在tableview中再添加一个自定义单元格,并添加一个标签,将其文本设置为
Load more record
,将其重用标识符设置为
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 

你可以在多种类型中使用它我在这里添加了3种类型,自定义你自己

第一选择

假设您从服务器获取所有数据(例如100),存储在some
NSMutableArray
,概念是声明一个全局
int

 int loadMoreItems;
在您的
视图中将出现

 -(void)viewWillAppear:(BOOL)animated
{
  loadMoreItems=21; // initially set the count is more than 20
 }

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
if ([yourTotalArrayName count]<=loadMoreItems) {
        return [yourTotalArrayName count];
    }
    else {
        return loadMoreItems;
    }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier;

 // add your data

 if (indexPath.row+1<loadMoreItems)
            {

      cell.lblLoadMoreItems.hidden=YES;
     cell.itemsdetails.text = [NSString stringWithFormat:@"%@",[yourTotalArrayName objectAtIndex:indexPath.row]] ;
     }
else

  {
      cell.itemsdetails.hidden=YES;
      cell.lblLoadMoreItems.text = @"Load more results...!";

  }
 }


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {

  if (indexPath.row+1<loadMoreItems) {
  // here do your stuff here 


   }
    else {
        loadMoreItems=loadMoreItems+20;
        [self.yourtableview reloadData];
    }
choiceNo 3

  use 2 prototype cell in `UItableView`

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

  return [yourarrayname count] + 1;
  }


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

 static NSString *postCellId = @"postCell";  
 static NSString *moreCellId = @"moreCell";
  UITableViewCell *cell = nil;



if ([indexPath row] == [yourarrayname count]) {

  cell = [tableView dequeueReusableCellWithIdentifier:moreCellId];

  if (cell == nil) {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:moreCellId];
  }

  cell.textLabel.text = @"Load more items...";
  cell.textLabel.textColor = [UIColor blueColor];


} 
else 
 {
  cell = [tableView dequeueReusableCellWithIdentifier:postCellId];
  if (cell == nil) {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:postCellId];
  }

   // show your Data
 }

return cell;
}



 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {

  if ([indexPath row] == [yourarrayname count]) {

    // call the web service and reload Data of UItableview
   }
 else

  {
    //normall selcftion procedure
 }

}

您必须将点击手势添加到标签中。之后,您将能够执行操作(在calback方法中执行任何您想要的操作)标签显示不正确…你可以阅读我的完整问题,还请看屏幕截图。我想在邮件加载后在邮件末尾显示标签或按钮。我想显示将其添加到你的tableview页脚。这可以在故事板中完成,也可以实用,这样你就不必担心约束或自动布局影响你的标签和框架origins@soulshined我找不到你,我只想在消息@Mishal Awan的末尾贴上这个标签。你是再次向服务器请求还是你已经在InboxMessagesRay中获得了所有数据?是的,我必须在iti上重新加载数据。我想在所有消息加载后显示这个页脚?@MishalAw你可以把它放在一个void函数中,当所有的消息都像你想的那样加载后调用它。这是很简单的部分。我不知道你如何设置tableView,但只要将它添加到你创建单元格的任何位置,一旦创建并加载完,然后显示FooterView等待我上传选项2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Classic start method

 // here add your data loaded

if (indexPath.row == [self.yourarrayName count] - 1)
{
    cell.lblLoadMoreItems.text = @"Load more results...!";
    [self launchReload];
 }
}



  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {

   if (indexPath.row == [self.yourarrayName count] - 1)
 {

    [self launchReload];
 }

 }



  -(void)launchRelaod

 {
 // call the webservice

  [self.yourtableview reloadData];
 }
  use 2 prototype cell in `UItableView`

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

  return [yourarrayname count] + 1;
  }


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

 static NSString *postCellId = @"postCell";  
 static NSString *moreCellId = @"moreCell";
  UITableViewCell *cell = nil;



if ([indexPath row] == [yourarrayname count]) {

  cell = [tableView dequeueReusableCellWithIdentifier:moreCellId];

  if (cell == nil) {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:moreCellId];
  }

  cell.textLabel.text = @"Load more items...";
  cell.textLabel.textColor = [UIColor blueColor];


} 
else 
 {
  cell = [tableView dequeueReusableCellWithIdentifier:postCellId];
  if (cell == nil) {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:postCellId];
  }

   // show your Data
 }

return cell;
}



 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {

  if ([indexPath row] == [yourarrayname count]) {

    // call the web service and reload Data of UItableview
   }
 else

  {
    //normall selcftion procedure
 }

}