为啥 UITableView 是空的?
Posted
技术标签:
【中文标题】为啥 UITableView 是空的?【英文标题】:Why UITableView is empty?为什么 UITableView 是空的? 【发布时间】:2014-03-05 12:02:05 【问题描述】:我想在 UITableView 中构建菜单,但我做错了什么。 我做错了什么?我得到空的 UITableView。我不知道,怎么了。也许有什么想法? 这是我的代码: .h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
IBOutlet UITableView *tblSimpleTable;
NSMutableArray *array1;
UIBarButtonItem *addButton;
BOOL manyCells;
UIView *headerView;
UITapGestureRecognizer *headerTapped;
NSMutableArray *history;
NSString *valueToSave;
NSMutableArray *sectionTitleArray;
NSMutableDictionary *sectionContentDict;
NSMutableArray *arrayForBool;
UITableView *table;
- (IBAction) EditTable:(id)sender;
@end
.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *arrayOfImages = [userDefaults objectForKey:@"tableViewDataImage"];
NSLog(@"test: %@", arrayOfImages);
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"news1.jpg"]];
addButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:self action:@selector(EditTable:)];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if (!sectionTitleArray)
sectionTitleArray = [NSMutableArray arrayWithObjects:@"Aachen", @"Berlin", @"Düren", @"Essen", @"Münster", @"Düren", @"Essen", @"Münster", @"Münster", nil];
if (!arrayForBool)
arrayForBool = [NSMutableArray arrayWithObjects:[NSNumber numberWithBool:NO],
[NSNumber numberWithBool:NO],
[NSNumber numberWithBool:NO],
[NSNumber numberWithBool:NO],
[NSNumber numberWithBool:NO],
[NSNumber numberWithBool:NO],
[NSNumber numberWithBool:NO],
[NSNumber numberWithBool:NO],
[NSNumber numberWithBool:NO] , nil];
if (!sectionContentDict)
sectionContentDict = [[NSMutableDictionary alloc] init];
if (arrayOfImages == NULL)
array1 = [NSMutableArray arrayWithObjects:@" Fakty", @" Sport", @" Rozrywka", @" Kultura", @" Biznes", @" deLuxe",@" Dom",nil];
else
array1 = arrayOfImages;
[sectionContentDict setValue:array1 forKey:[sectionTitleArray objectAtIndex:0]];
// NSArray *array2 = [NSArray arrayWithObjects:@"wurst 1", @"käse 2", @"keks 3", nil];
// [sectionContentDict setValue:array2 forKey:[sectionTitleArray objectAtIndex:1]];
table = [[UITableView alloc]initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:table];
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return [sectionTitleArray count];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
if ([[arrayForBool objectAtIndex:section] boolValue])
return [[sectionContentDict valueForKey:[sectionTitleArray objectAtIndex:section]] count];
return 0;
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
headerView.tag = section;
headerView.backgroundColor = [UIColor clearColor];
UILabel *headerString = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 120, 50)];
headerString.backgroundColor = [UIColor clearColor];
manyCells = [[arrayForBool objectAtIndex:section] boolValue];
if (section == 0)
headerString.text = @"Wiadomości";
headerTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
[headerView addGestureRecognizer:headerTapped];
//up or down arrow depending on the bool
UIImageView *upDownArrow = [[UIImageView alloc] initWithImage:manyCells ? [UIImage imageNamed:@"upArrowBlack"] : [UIImage imageNamed:@"downArrowBlack"]];
upDownArrow.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
upDownArrow.frame = CGRectMake(150, 10, 25, 25);
upDownArrow.backgroundColor = [UIColor clearColor];
[headerView addSubview:upDownArrow];
else if (section == 1)
headerString.text = @"Firmy";
headerTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTappedd:)];
[headerView addGestureRecognizer:headerTapped];
else if (section == 2)
headerString.text = @"Imprezy";
else if (section == 3)
headerString.text = @"Kino";
else if (section == 4)
headerString.text = @"Komunikacja";
else if (section == 5)
headerString.text = @"Pogoda";
else if (section == 6)
headerString.text = @"Ogłoszenia";
else if (section == 7)
headerString.text = @"Poinformuj nas";
else if (section == 8)
headerString.text = @"Ustawienia";
headerString.textAlignment = NSTextAlignmentLeft;
headerString.textColor = [UIColor blackColor];
[headerView addSubview:headerString];
return headerView;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
return 50;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
// ]if ([[arrayForBool objectAtIndex:indexPath.section] boolValue])
return 44;
//
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
// return 1;
//
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// BOOL manyCells = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
// if (!manyCells)
// cell.textLabel.text = @"click to enlarge";
//
// else
NSArray *content = [sectionContentDict valueForKey:[sectionTitleArray objectAtIndex:indexPath.section]];
cell.textLabel.text = [content objectAtIndex:indexPath.row];
//
cell.backgroundColor = [UIColor clearColor];
return cell;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// DetailViewController *dvc;
// if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
// dvc = [[DetailViewController alloc] initWithNibName:@"DetailViewController_iPhone" bundle:[NSBundle mainBundle]];
// else
// dvc = [[DetailViewController alloc] initWithNibName:@"DetailViewController_iPad" bundle:[NSBundle mainBundle]];
//
// dvc.title = [sectionTitleArray objectAtIndex:indexPath.section];
// dvc.detailItem = [[sectionContentDict valueForKey:[sectionTitleArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
// [self.navigationController pushViewController:dvc animated:YES];
#pragma mark - gesture tapped
- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer
if (manyCells)
self.navigationItem.leftBarButtonItem = nil;
[super setEditing:NO animated:NO];
[tblSimpleTable setEditing:NO animated:NO];
[tblSimpleTable reloadData];
else
[self.navigationItem setLeftBarButtonItem:addButton];
[self.navigationItem.leftBarButtonItem setTitle:@"Edycja"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
[super setEditing:NO animated:NO];
[tblSimpleTable setEditing:NO animated:NO];
[tblSimpleTable reloadData];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];
if (indexPath.row == 0)
BOOL collapsed = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
collapsed = !collapsed;
[arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber numberWithBool:collapsed]];
//reload specific section animated
NSRange range = NSMakeRange(indexPath.section, 1);
NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
[table reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationFade];
- (IBAction) EditTable:(id)sender
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[table scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];
if(self.editing)
table.scrollEnabled = YES;
[super setEditing:NO animated:NO];
[tblSimpleTable setEditing:NO animated:NO];
[tblSimpleTable reloadData];
[self.navigationItem.leftBarButtonItem setTitle:@"Edycja"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
else
table.scrollEnabled = NO;
[super setEditing:YES animated:YES];
[tblSimpleTable setEditing:YES animated:YES];
[tblSimpleTable reloadData];
[self.navigationItem.leftBarButtonItem setTitle:@"Gotowe"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
// The editing style for a row is the kind of button displayed to the left of the cell when in editing mode.
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
return UITableViewCellEditingStyleNone;
- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
return NO;
#pragma mark Row reordering
// Determine whether a given row is eligible for reordering or not.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
return YES;
// Process the row move. This means updating the data model to correct the item indices.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSArray *arrayOfImages = [userDefaults objectForKey:@"tableViewDataImage"];
NSLog(@"tablicansuser: %@", arrayOfImages);
NSString *item = [array1 objectAtIndex:fromIndexPath.row];
[array1 removeObject:item];
[array1 insertObject:item atIndex:toIndexPath.row];
NSLog(@"tablica: %@", array1);
[userDefaults setObject:array1 forKey:@"tableViewDataImage"];
[userDefaults synchronize];
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
if( sourceIndexPath.section != proposedDestinationIndexPath.section )
return sourceIndexPath;
else
return proposedDestinationIndexPath;
- (void)sectionHeaderTappedd:(UITapGestureRecognizer *)gestureRecognizer
// DetailViewController *dvc;
// dvc = [[DetailViewController alloc] initWithNibName:@"DetailViewController_iPhone" bundle:[NSBundle mainBundle]];
// [self.navigationController pushViewController:dvc animated:YES];
@end
帮帮我:)
【问题讨论】:
你给[table reloadData]
打过电话吗??是否调用了您的 dataSource 方法?
您忘记设置table
的delegate
和dataSource
。然后检查您的numberOfRowsInSection
是否被调用以及从那里返回什么值?
对于任何 NSMutableArray,使用 [[NSMutableArray alloc] initWitharrayWithObj...
设置UITableview的table view delegate和datasource
在哪里写 [table reloadData]?
【参考方案1】:
写入h文件
UITableViewDelegate,UITableViewDataSource
并在 .m 文件中
table = [[UITableView alloc]initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
table.Delegate=self;
table.DataSource=self;
[self.view addSubview:table];
【讨论】:
嘿,您在 .h 文件中声明了两个 TableView 那么您要在哪个表上显示数据.....【参考方案2】:你必须在viewDidload方法中设置tableview委托方法
tblView.delegate = self;
tblView.dataSource = self;
还有一件事,你必须在头文件中设置UITableViewDataSource, UITableViewDelegate。
【讨论】:
以上是关于为啥 UITableView 是空的?的主要内容,如果未能解决你的问题,请参考以下文章