uitableviewcell中的多个倒数计时器无法正常工作

时间:2014-01-15 01:23:05

标签: objective-c uitableview ios7 countdowntimer

这是我第一次试用iOS的应用程序,所以我需要一些帮助。

我的问题是:我在视图中有一个表视图单元格,每个单元格中都有自定义标签。通过添加它们作为子视图以编程方式创建标签(我这样做是因为形式我读的是唯一的方式)。在每个单元格中,我有一些数据和倒数计时器(例如12:03:23),需要倒计时到00:00:00之后,我会表现出悲伤的表情。问题是我无法让所有倒数计时器减少上次计时器工作的时间。

以下是我的代码格式.m文件的一部分:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

[UIColor clearColor];

static NSString *CELL_ID2 = @"SeparatorID";
// even rows will be invisible
if (indexPath.row % 2 == 1)
{
    [UIColor clearColor];
    UITableViewCell * cell2 = [tableView dequeueReusableCellWithIdentifier:CELL_ID2];

    if (cell2 == nil)
    {
        cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELL_ID2];
        [cell2.contentView setAlpha:0];
        cell2.backgroundColor = [UIColor clearColor];
        cell2.backgroundView = [UIView new];
        cell2.selectedBackgroundView = [UIView new];
        [cell2 setUserInteractionEnabled:NO]; // prevent selection and other stuff
    }
    return cell2;
}

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

AppFeed *appFeed = [self.appFeeds objectAtIndex:indexPath.row/2];

cell.backgroundColor = [UIColor clearColor];
cell.backgroundView = [UIView new];
cell.selectedBackgroundView = [UIView new];


cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"redLabel.png"]];

**... Here is some missing repeated label code which is not useful**

//Create subview countDown label

textLabelCountDown.tag = 1;
textLabelCountDown = [[UILabel alloc] initWithFrame:CGRectMake(142, 53, 130, 30)];
textLabelCountDown.numberOfLines = 1;
textLabelCountDown.lineBreakMode = NSLineBreakByTruncatingTail;
textLabelCountDown.textAlignment = NSTextAlignmentRight;
textLabelCountDown.textColor = [UIColor whiteColor];
textLabelCountDown.text = @":(";

textLabelCountDown.font = [UIFont fontWithName:@"Helvetica Bold" size:28];
textLabelCountDown.backgroundColor = [UIColor clearColor];
[self performSelector:@selector(countDownDuration) withObject:nil afterDelay:1];
[cell.contentView addSubview:textLabelCountDown];

return cell;

}

这是我的另一部分代码:void coundDownTimer:

- (void)countDownDuration {

NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
AppFeed *appFeed = [self.appFeeds objectAtIndex:indexPath.row/2];

NSDate *sourceDate = [NSDate date];

NSTimeZone *sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone *destinationTimeZone = [NSTimeZone systemTimeZone];

NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

NSDate *now = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];


NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-DD HH:mm:ss zzz"];
NSDate *startingDate = [dateFormatter dateFromString: [NSString stringWithFormat:@"%@ %@ %@", appFeed.start_date, appFeed.start_time, @" +0000"]];
NSDate *endingDate = [dateFormatter dateFromString: [NSString stringWithFormat:@"%@ %@ %@", appFeed.start_date, appFeed.end_time, @" +0000"]];


NSTimeInterval distanceBetweenDates = [endingDate timeIntervalSinceDate: now];

div_t h = div(distanceBetweenDates, 3600);
int hours = h.quot;
div_t m = div(h.rem, 60);
int minutes = m.quot;
int seconds = m.rem;

int days = 1;

NSComparisonResult resultStarting;
resultStarting = [now compare:startingDate];

if(resultStarting == NSOrderedAscending) {
    if(days == 0){
        [textLabelCountDown setText: [NSString stringWithFormat: @"%d Zi", days]];
    } else {
        [textLabelCountDown setText: [NSString stringWithFormat: @"%d:%02d:%02d", hours, minutes, seconds]];
    }
} else if(resultStarting == NSOrderedDescending) {

    [textLabelCountDown setText: @":("];

} else {
    [textLabelCountDown setText: [NSString stringWithFormat: @"%d:%02d:%02d", hours, minutes, seconds]];
}

[self performSelector:@selector(countDownDuration) withObject:nil afterDelay:1];    
}

此代码的一部分是表单stackoverflow形式简单的cowntdwon计时器。 我的代码出了什么问题?

我需要的是时间减少,之后我需要显示文本而不是时间。

PS:我的代码没有任何通知或编译错误。我试着把代码

[self performSelector:@selector(countDownDuration) withObject:nil afterDelay:1];

在viewDidLoad中它并没有起作用。我尝试过:

[cell performSelector:@selector(countDownDuration) withObject:nil afterDelay:1];
返回单元格之前

; ,我收到此错误:

 2014-01-15 03:15:29.027 App[1220:60b] -[UITableViewCell countDownDuration]:  unrecognized selector sent to instance 0x1658fbd0
 2014-01-15 03:15:29.030 App[1220:60b] *** Terminating app due to uncaught exception   'NSInvalidArgumentException', reason: '-[UITableViewCell countDownDuration]: unrecognized  selector sent to instance 0x1658fbd0'
 *** First throw call stack:
 (0x2ec77f4b 0x390b86af 0x2ec7b8e7 0x2ec7a1cb 0x2ebc94d8 0x2f65238b 0x2ec430df 0x2ec42cf7  0x2ec41093 0x2ebabc27 0x2ebaba0b 0x338d2283 0x3144f049 0x66909 0x395c0ab7)
 libc++abi.dylib: terminating with uncaught exception of type NSException
 (lldb) 

请求帮助我。提前谢谢。

0 个答案:

没有答案