扩展UITableView Cell无法正常工作

时间:2014-06-06 14:57:12

标签: ios objective-c uitableview

我试图重新创建手风琴桌面视图,其中一个单元格在点击时展开以显示另一个隐藏的单元格,并在再次点击时收缩。此tableview是另一个视图中的子视图。我真的坚持这个,我真的不知道出了什么问题,但输出是空白的。

非常感谢任何帮助和启发。

标题

#import <UIKit/UIKit.h>

@interface CardViewController : UIViewController{
    NSTimer *stopTimer;
    NSDate *startDate;
    BOOL running;
    NSTimeInterval secondsAlreadyRun;

    NSArray *topItems;
    NSMutableArray *subItems; // array of arrays

    int currentExpandedIndex;
}

@property (strong, nonatomic) IBOutlet UILabel *timerLabel;
@property (strong, nonatomic) UIButton *timerPressed;
@property (strong, retain) UITableView *tableView;

- (IBAction)timerPressed:(id)sender;
- (IBAction)stopPressed:(id)sender;
- (void) updateTimer;

@end

实施

#import "CardViewController.h"

#define NUM_TOP_ITEMS 2
#define NUM_SUBITEMS 2

@interface CardViewController (){
    NSTimeInterval _lastTimerInterval;
}

@end

@implementation CardViewController

@synthesize timerLabel;


#pragma  mark - main view

- (void)viewDidLoad
{
    [super viewDidLoad];
    running = false;

    topItems = [[NSArray alloc] initWithArray:[self topLevelItems]];
    subItems = [NSMutableArray new];
    currentExpandedIndex = -1;

    for (int i = 0; i < [topItems count]; i++) {
        [subItems addObject:[self subItems]];
    }
}


#pragma mark - Data generators

- (NSArray *)topLevelItems {
    NSMutableArray *items = [NSMutableArray array];

    for (int i = 0; i < NUM_TOP_ITEMS; i++) {
        [items addObject:[NSString stringWithFormat:@"Item %d", i + 1]];
    }

    return items;
}

- (NSArray *)subItems {
    NSMutableArray *items = [NSMutableArray array];
    int numItems = arc4random() % NUM_SUBITEMS + 2;

    for (int i = 0; i < numItems; i++) {
        [items addObject:[NSString stringWithFormat:@"SubItem %d", i + 1]];
    }

    return items;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [topItems count] + ((currentExpandedIndex > -1) ? [[subItems objectAtIndex:currentExpandedIndex] count] : 0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ParentCellIdentifier = @"ParentCell";
    static NSString *ChildCellIdentifier = @"ChildCell";

    BOOL isChild =
    currentExpandedIndex > -1
    && indexPath.row > currentExpandedIndex
    && indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];

    UITableViewCell *cell;

    if (isChild) {
        cell = [tableView dequeueReusableCellWithIdentifier:ChildCellIdentifier];
    }
    else {
        cell = [tableView dequeueReusableCellWithIdentifier:ParentCellIdentifier];
    }


    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ParentCellIdentifier];
    }

    if (isChild) {
        cell.detailTextLabel.text = [[subItems objectAtIndex:currentExpandedIndex] objectAtIndex:indexPath.row - currentExpandedIndex - 1];
    }
    else {
        int topIndex = (currentExpandedIndex > -1 && indexPath.row > currentExpandedIndex)
        ? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count]
        : indexPath.row;

        cell.textLabel.text = [topItems objectAtIndex:topIndex];
        cell.detailTextLabel.text = @"";
    }

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BOOL isChild =
    currentExpandedIndex > -1
    && indexPath.row > currentExpandedIndex
    && indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];

    if (isChild) {
        NSLog(@"A child was tapped, do what you will with it");
        return;
    }

    [self.tableView beginUpdates];

    if (currentExpandedIndex == indexPath.row) {
        [self collapseSubItemsAtIndex:currentExpandedIndex];
        currentExpandedIndex = -1;
    }
    else {

        BOOL shouldCollapse = currentExpandedIndex > -1;

        if (shouldCollapse) {
            [self collapseSubItemsAtIndex:currentExpandedIndex];
        }

        currentExpandedIndex = (shouldCollapse && indexPath.row > currentExpandedIndex) ? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count] : indexPath.row;

        [self expandItemAtIndex:currentExpandedIndex];
    }

    [self.tableView endUpdates];

}

- (void)expandItemAtIndex:(int)index {
    NSMutableArray *indexPaths = [NSMutableArray new];
    NSArray *currentSubItems = [subItems objectAtIndex:index];
    int insertPos = index + 1;
    for (int i = 0; i < [currentSubItems count]; i++) {
        [indexPaths addObject:[NSIndexPath indexPathForRow:insertPos++ inSection:0]];
    }
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

}

- (void)collapseSubItemsAtIndex:(int)index {
    NSMutableArray *indexPaths = [NSMutableArray new];
    for (int i = index + 1; i <= index + [[subItems objectAtIndex:index] count]; i++) {
        [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
    }
    [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];

}


- (IBAction)timerPressed:(id)sender {
    if (!running){

        running = true;
//        [sender setTitle:@"Pause" forState:UIControlStateNormal];
        if (stopTimer == nil){
            stopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                         target:self
                                                       selector:@selector(updateTimer)
                                                       userInfo:nil
                                                        repeats:YES];
            startDate = [NSDate date];
        }
    }
    else {
        running = false;
        [stopTimer invalidate];
        stopTimer = nil;
        secondsAlreadyRun += [[ NSDate date] timeIntervalSinceDate:startDate];



    }
}


- (IBAction)stopPressed:(id)sender {
    [stopTimer invalidate];
    self.timerPressed.userInteractionEnabled = NO;
    self.timerLabel.enabled = NO;
    int timeDifference = (600 - _lastTimerInterval);
    int sec = timeDifference % 60;
    int min = floor((timeDifference/60));
    NSString *finalTime =  [NSString stringWithFormat:@"%d" @":" @"%d", min, sec];
    NSLog(@"%@",finalTime);

    //slight uncorrected issue, if press timer twice will restart.
}

-(void)updateTimer{
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
    timeInterval += secondsAlreadyRun;
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"mm:ss"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    NSString *timeString=[dateFormatter stringFromDate:timerDate];
    timerLabel.text = timeString;
    _lastTimerInterval = timeInterval;
}

@end

0 个答案:

没有答案