2014-03-05 74 views
0

我想在UITableView中建立菜單,但我做錯了什麼。 我做錯了什麼?我得到空的UITableView。我不知道,什麼是錯的。也許有一些想法?
這是我的代碼:

.H
爲什麼UITableView是空的?

#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 

幫我:)

+1

你有沒有調用過'[table reloadData]'?你的dataSource方法被調用了嗎? –

+1

你忘了設置'table'的'delegate'和'dataSource'。 然後檢查你的'numberOfRowsInSection'被調用,並從那裏返回什麼值? –

+0

對於任何NSMutableArray,請使用[[NSMutableArray alloc] initWitharrayWithObj ... –

回答

0

你必須設置的tableview委託方法在viewDidLoad中方法

tblView.delegate = self; 
tblView.dataSource = self; 

另外一件事,你必須設置UITableViewDataSource,的UITableViewDelegate頭文件中的

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]; 
+0

我寫,但沒有工作 – wysio90

+0

嘿你聲明兩個TableView在你的.h文件所以在哪個表上你想顯示數據..... –