ios - tableView为空时如何显示标签

标签 ios uitableview uilabel

我想在 tableView 为空时显示一个标签。 我已经尝试过此页面上提出的其他解决方案,但没有一个对我有用。我想说“按添加按钮创建一个新类”之类的话。我的 TableviewController 是:

#import "ClasesTableViewController.h"
#import "ClasesTableViewCell.h"
#import "ViewController.h"

@interface ClasesTableViewController ()

@end

@implementation ClasesTableViewController

@synthesize clases;

- (NSManagedObjectContext *)managedObjectContext {
    NSManagedObjectContext *context  = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.navigationItem setTitle:@"Clases"]; /*Cambia el titulo del navigation controller*/

    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}]; /*Cambia el color de las letras del navigation controller bar del menu principal*/

    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:62/255.0f green:152/255.0f blue:73/255.0f alpha:1.0f]];

    self.TableView.tableFooterView = [[UIView alloc] init]; /*Esta linea hace que en la tabla solo aparezcan el numero de filas que tienes establecidas, es decir, que las vacias no aparezcan*/

    self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; /*Cambia el color del boton de la izquierda*/

    UIBarButtonItem *infoBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"info_white48@2x.png"] style:UIBarButtonItemStylePlain target:self  action:@selector(infoAction)];

    self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects: _AddBarButton, infoBarButtonItem, nil];


}


-(void)infoAction
{
    NSInteger clasesDadas = clases.count;

    NSString *inStr = [NSString stringWithFormat:@"\nClases dadas: %d", (int)clasesDadas];


    UIAlertView *alertatiempo = [[UIAlertView alloc] initWithTitle:@"Información" message:inStr delegate:self cancelButtonTitle:@"Aceptar" otherButtonTitles:nil];
    [alertatiempo show];

}



- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"UpdateClase"]) {
        NSManagedObject *selectedClase = [clases objectAtIndex:[[self.TableView indexPathForSelectedRow] row]];
        ClasesViewController *destViewController = segue.destinationViewController;
        destViewController.clase = selectedClase;
        destViewController.numberOfClasses = clases.count;
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    NSManagedObjectContext *moc = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Clases"];
    clases = [[moc executeFetchRequest:fetchRequest error:nil] mutableCopy];

    [self.tableView reloadData];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return clases.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    /*UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];*/
    ClasesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


    //Configure the cell
    NSManagedObject *clase = [clases objectAtIndex:indexPath.row];
    [cell.dateLabel setText:[NSString stringWithFormat:@"%@", /*countString,*/ [clase valueForKey:@"date"]/*, [clase valueForKey:@"date"]*/]];
    [cell.timeLabel setText:[clase valueForKey:@"time"]];
    [cell.paidLabel setText:[clase valueForKey:@"paid"]];
    cell.paidImage.image = [UIImage imageNamed:@" "];

    if ([cell.paidLabel.text isEqualToString:@"No"] || [cell.paidLabel.text isEqualToString:@"no"]) {
        cell.paidLabel.textColor = [UIColor redColor];
    }
    if ([cell.paidLabel.text isEqualToString:@"Si"] || [cell.paidLabel.text isEqualToString:@"si"]) {
        cell.paidLabel.textColor = [UIColor greenColor];
        cell.paidImage.image =[UIImage imageNamed:@"paid_green@2x.png"];
    }

    return cell;

}



 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

     // Return NO if you do not want the specified item to be editable.
     return YES;

 }


 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

     NSManagedObjectContext *context = [self managedObjectContext];

     if (editingStyle == UITableViewCellEditingStyleDelete) {
         // Delete the row from the data source
         [context deleteObject:[clases objectAtIndex:indexPath.row]];

         //invoke the "save" method to commit the change
         NSError *error = nil;
         if (![context save:&error]) {
              NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
             return;
         }

         //Remove clase from table view
         [clases removeObjectAtIndex:indexPath.row];
         [self.TableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

     }
 }

@end

谁能帮帮我?非常感谢。

最佳答案

一个简单的解决方案是使用其他自定义 UITableViewCell 来显示空状态。

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
{
    return clases.count>0?clases.count:1;  //Return 1 if clases array is empty 
}

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
  if(clases.count==0 && indexPath.row == 0)
  {
    //return a custom cell  with the label "Press the add button to create a new class"
  }
  else
  {
    //return an instance of ClasesTableViewCell as you was doing in your code which represents one class
  }
}

关于ios - tableView为空时如何显示标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29575821/

相关文章:

iphone - 如何在 Obj C 的同一 View 屏幕中删除一个表中的滑动删除按钮而不是另一个表中的滑动删除按钮?

ios - 如何使用 Storyboard 设置包装文本 UILabel Xcode

ios - 带有 PushKit 的 SIP 服务器/提供商

iOS 在 4 英寸设备上运行 3.5 英寸应用程序

ios - UITableView 的属性可以是 UITableView Delegate 和 DataSource

ios - TableView 单元格颜色

ios - 一些 UI 元素不获取 UIAppearance 特征

ios - 无论文本如何,UILabel 都会将大小调整为 0 的高度

ios - 在 iOS 中后台上传照片超过 10 分钟

ios - 当我折叠/展开屏幕时,displayModeButtonItem 消失