为什么UITableView为空?

召唤90

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

帮我:)

尼尔玛辛(Nirmalsinh)

您必须viewDidload方法中设置tableview委托方法

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

还有一件事,您必须在头文件中设置UITableViewDataSource,UITableViewDelegate

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章