从自定义UITableViewCell中的FirstViewController访问属性

时间:2014-06-11 16:59:08

标签: ios xcode uitableview properties import

在我的iOS应用中,我有一个自定义UITableViewCell,它是我FirstViewController中TableView的一部分。在FirstViewController中,我有一个对象数组,对应于填充行,然后是该数组中具有属性的对象。

我在每个单元格中都有一个计时器,我需要从相应对象的属性中获取计时器的长度。我尝试导入FirstViewController.h,但收到错误" property not found on object of type"无论如何。

我已经在FirstViewController的cellForRowAtIndexPath方法中创建和配置了对象,但我希望在TableViewCell.m的方法中使用它。

有没有办法可以在TableViewCell中使用数组或对象?或者该方法应该在FirstViewController中实现吗?

编辑#1:

我将cell.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:cell selector:@selector(startTimer) userInfo:nil repeats:YES];移动到FirstViewController的cellForRow...方法中。现在计时器以所需的时间运行,但它不再由按钮启动。它在加载TableView时立即运行,并在每次重新加载TableView时重新启动。

以下是我的ATCTableViewCell.h文件:(使用编辑#2更新)

#import <UIKit/UIKit.h>
#import "ATCFirstViewController.h"

@interface ATCTableViewCell : UITableViewCell

- (void)startTimer;
// Contents moved to cellForRow... -> - (IBAction)playTimer:(id)sender; 

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UIButton *playButton;
@property (weak, nonatomic) IBOutlet UIButton *pauseButton;

@property NSTimer *timer;
@property int secondsCount; // initially the length in seconds and then decreased

@end

编辑#2:

计时器是递减计时器,用于更改startTimerATCTableViewCell.m方法中的标签。标签是此刻单元格的属性以及另一个标签和按钮。我会将事物移动到对象类,而不是将它们作为单元格的属性。

以下是startTimer方法:

- (void)startTimer {

    self.secondsCount--;
    int hours = self.secondsCount / 3600;
    int minutes = (self.secondsCount / 60) - (hours * 60);
    int seconds = self.secondsCount - (hours * 3600) - (minutes * 60);

    NSString *timerOutput = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
    self.timeLabel.text = timerOutput;

    if (self.secondsCount == 0) {
        [self.timer invalidate];
        self.timer = nil;
    }
}

这是我的ATCObject.h文件:

#import <Foundation/Foundation.h>

@interface ATCObject : NSObject

@property NSString *title;

@property int lengthInSeconds;
@property int initialHours;
@property int initialMinutes;

@end

由于

1 个答案:

答案 0 :(得分:0)

我正在根据之前的所有讨论更新此答案。

@interface ATCObject : NSObject
@property (nonatomic, strong) NSString *title;
@property (nonatomic, assign) NSInteger lengthInSeconds;
@property (nonatomic, assign) NSInteger initialHours;
@property (nonatomic, assign) NSInteger initialMinutes;
//Move the NSTimer and the -playTimer method to this class
@property (nonatomic,strong) NSTimer *timer;
-(void)startTimer;
-(void)timerTick;
-(void)pauseTimer;
@end

在tableView控制器类中:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Identifier";
    ATCTableViewCell *cell = (ATCTableViewCell *)[tableView dequeueReusableCellForIdentifer:Identifier];
    if (!cell) {
        cell = [[ATCTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault identifier:Identifier];
        [cell.playButton addTarget:self action:@selector(playTouched:) forControlEvents:UIControlEventTouchUpInside];
        [cell.pauseButton addTarget:self action:@selector(pauseTouched:) forControlEvents:UIControlEventTouchUpInside];
    }
    ATCObject *objectForRow = [myDataArray objectAtIndex:indexPath.row];
    cell.timeLabel.text = objectForRow.title; 
    return cell;
}

//Also in your controller class
-(void)playTouched:(id)sender {
    //you may need to change self.tableView to however you reference the table in your class
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    if (indexPath != nil) {
        ATCObject *currentObject = [myDataArray objectAtIndex:indexPath.row];
        [currentObject startTimer];
    }
}

//ATCObject.m
-(void)startTimer {
    if (!self.timer) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                     target:self
                     selector:@selector(timerTick:)
                     userInfo:nil
                     repeats:YES];  
    }
}

-(void)timerTick {
    //Already assuming lengthInSeconds is set
    if (lengthInSeconds > 0) {
        lengthInSeconds--;
        //Do your math to get total hours and minutes
        NSInteger totalHours = //math stuff convert lengthInSeconds
        NSInteger totalMinutes = //more math stuff etc
        NSString *newTitle = [NSString stringWithFormat:@"%i:%i",totalHours,totalMinutes];
        self.title = newTitle;
    } else {
        self.title = @"00:00";
        [self.timer invalidate];
        self.timer = nil;
    }
}