我仍在学习 iOS,这是我的第一个主要项目,所以请放轻松。我正在尝试制作一个手势驱动的任务列表。我还有很多工作要做,但我希望你们能帮助我完成这项工作。我确定我正确设置了委托(delegate)和数据源,但它仍然没有显示...无论如何,这是我的 ViewController.m 文件。
提前致谢!
#import "AGViewController.h"
#import "AGTaskObject.h"
#import "AGTableViewCell.h"
//#import "AGSettingsViewController.h"
@interface AGViewController ()
@end
@implementation AGViewController {
NSMutableArray *taskItems;
}
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
//Initializing the Array
taskItems = [[NSMutableArray alloc] init];
[taskItems addObject:[AGTaskObject toDoItemWithText:@"Tap the + to add a Task"]];
[taskItems addObject:[AGTaskObject toDoItemWithText:@"Tap the Gear Button to check out settings"]];
[taskItems addObject:[AGTaskObject toDoItemWithText:@"Swipe Right to check off a task"]];
[taskItems addObject:[AGTaskObject toDoItemWithText:@"Swipe Left to Delete a Task"]];
[taskItems addObject:[AGTaskObject toDoItemWithText:@"I <3 Bacon"]];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Table View Delegates
self.tableView.delegate = self;
self.tableView.dataSource = self;
//set the initial background image
self.tableView.backgroundColor = [UIColor clearColor];
self.backgroundImage.image = [UIImage imageNamed:@"blue.png"];
[self.tableView registerClass:[AGTableViewCell class] forCellReuseIdentifier:@"cell"];
//Get rid of empty cells at bottom of tableview
// self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
// Long Press Gesture to rearrange cells
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureRecognized:)];
[self.tableView addGestureRecognizer:longPress];
// // Tap Gesture to add a new cell
// UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(<#selector#>)];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Button Press (IB)Actions
- (IBAction)settingsButtonPressed:(UIButton *)sender
{
[self performSegueWithIdentifier:@"toSettingsViewController" sender:nil];
}
- (IBAction)addTaskButtonPressed:(UIButton *)sender
{
}
#pragma mark - UITableView Configuration
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [taskItems count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"cell";
//re-Use or create a tableview cell
AGTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier
forIndexPath:indexPath];
/* if (cell == nil) {
cell = [[AGTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
*/
//Make the cells clear to show the background image
cell.backgroundColor = [UIColor clearColor];
//default font color will be white
cell.textLabel.textColor = [UIColor whiteColor];
//Set the task for each cell in the tableview
AGTaskObject *item = taskItems[indexPath.row];
//set the text
cell.delegate = self;
cell.taskObject = item;
// Recall final cell
return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [self colorForIndex:indexPath.row];
}
#pragma mark - TableItem Delete
-(void)toDoItemDeleted:(id)taskobject {
// use the UITableView to animate the removal of this row
NSUInteger index = [taskItems indexOfObject:taskobject];
[self.tableView beginUpdates];
[taskItems removeObject:taskobject];
[self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]]
withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}
最佳答案
Storyboard不使用 initWithNibName(即使 Xcode 毫无意义地创建它)所以您的数组是空的。尝试将其移动到 viewDidLoad
关于ios - UITableview 不显示内容/空单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23096047/