在uitableviewcell中创建UIlabel的动作

时间:2014-12-22 05:19:25

标签: ios objective-c uitableview

我正在我的应用程序中创建一个收件箱模块。首次加载20条消息来自服务器。 我希望在20条消息之后,一个标签名为"加载更多消息"将在UItableview单元格中创建。 点击该标签后,我想再次拨打服务器。 我曾尝试过以下代码,但它无效。提前thanx :( 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;
}

5 个答案:

答案 0 :(得分:1)

尝试使用Methods

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");
}  

答案 1 :(得分:1)

由于您希望将一行添加到显示Load More Records的tableview,您可以通过添加页脚视图或每个最后一行,通过在方法{{1}中传递一个额外的行来以2种方式执行此操作并在-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

中返回此行的特定单元格

例如在tableview中添加一个自定义单元格并添加标签并将其文本设置为-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath并将其重用标识符设置为Load more record

LabelCell

答案 2 :(得分:0)

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

tableview.tableFooterView = label;

通过这种方式,您将始终在桌子末尾获得标签。

为了加载更多操作,请在标签上添加点按手势。

答案 3 :(得分:0)

如果您只想在桌面的底部或下方显示某些内容,请执行快速步骤:

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

UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 25)];
UIButton *loadMoreButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 25)];
[loadMoreButton setTitle:@"Load More" forState:UIControlStateNormal];
[loadMoreButton addTarget:self action:@selector(loadMore:) forControlEvents:UIControlEventTouchUpInside];
//More customizing
[footerView addSubview:loadMoreButton];
self.tableView.tableFooterView = footerView;

-(IBAction)loadMoreButton:(id)sender {
//load more from server 
}

答案 4 :(得分:0)

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

选择否1

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

 int loadMoreItems;

viewWillAppear

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

选择否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];
 }

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
 }

}
相关问题