gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:24:43 27 4
gpt4 key购买 nike

我想在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com