菜鸟教程小白 发表于 2022-12-12 13:19:33

ios - 在 uitableviewcell 中创建 UIlabel 的 Action


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

<pre><code>-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return +1;
}

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

    static NSString *tableviewidentifier = @&#34;cell&#34;;
   __blocktablecellTableViewCell *cell= ;

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



    }
    else{

   __block NSString *row = ;

    cell.titlename.font=;
    cell.tolbl.font=;
    cell.fromlbl.font=;
    cell.datelbl.font=;
    cell.timelbl.font=;
      if([[objectForKey:@&#34;messageRead&#34;] intValue]==0)
    {

      cell.titlename.font=;
      cell.tolbl.font=;
      cell.fromlbl.font=;
      cell.datelbl.font=;
      cell.timelbl.font=;

                cell.contentView.backgroundColor=;



    }
    else
    {
      cell.titlename.font=;
      cell.tolbl.font=;
      cell.fromlbl.font=;
      cell.datelbl.font=;
      cell.timelbl.font=;

      cell.contentView.backgroundColor=;

    }
    cell.titlename.text=[objectForKey:@&#34;offerTitle&#34;];
   //toCity

    cell.tolbl.text=[objectForKey:@&#34;toCity&#34;];
    cell.fromlbl.text=[objectForKey:@&#34;fromCity&#34;];
    if(![ 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=[objectForKey:@&#34;offerPhoto&#34;];// here i am getting image path
            NSURL *url = ;
            NSData * imageData = ;
            UIImage *image = ;

            dispatch_sync(dispatch_get_main_queue(), ^{ //in main thread update the image
               ;
                cell.profileimage.image = image;
                cell.textLabel.text = @&#34;&#34;; //add this update will reflect the changes
                NSLog(@&#34;loading and adding to dictionary&#34;);
            });
      });
    }
    else
    {
      cell.profileimage.image = ;
      NSLog(@&#34;retriving from dictionary&#34;);
    }


    cell.datelbl.text=[objectForKey:@&#34;messageDate&#34;];
    cell.timelbl.text=[objectForKey:@&#34;messageTime&#34;];
    }

return cell;
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>由于您希望将一行添加到显示 <code>Load More Records</code> 的 tableview 中,您可以通过添加页脚 View 或每个最后一行,通过在方法 <code>-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section</code> 并在 <code>-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath 中返回此行的特定单元格:(NSIndexPath *)indexPath</code> </p>

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

<pre><code>-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return self.inboxmessagesarray.count + 1 ;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
   //;
   if(indexPath.row == )
   {
      NSLog(@&#34;load more records&#34;);
   }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
__blocktablecellTableViewCell *cell= ;
if(cell == nil)
{
      cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableviewidentifier];
}
//in the last row
if(indexPath.row == self.inboxmessagesarray.count)
{
      cell = ;
      if(cell == nil)
      {
          cell = [initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@&#34;LabelCell&#34;];
      }
      if( &gt; 0)
          cell.hidden = NO;
      else
          cell.hidden = YES;

      return cell;
}
//..rest of the code
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 uitableviewcell 中创建 UIlabel 的 Action ,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/27596953/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/27596953/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 uitableviewcell 中创建 UIlabel 的 Action