0

私のアプリでは、UITableView で週の日付を表示したいと考えています。以下のコードを試していますが、すべての行で同じ日付を繰り返しています。実際に表示したいのは UITableView のカレンダーの日付ですが、週のバイスです。以下は私が試しているコードです。私の主張を明確にしたことを願っています。疑問がある場合は質問してください。

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell;

if(tableView == self.mWeekTable)
{
    static NSString *cellIdentifier = @"Cell";
    cell = [self.mLocationTable dequeueReusableCellWithIdentifier:cellIdentifier];

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

    NSDate *dateForCell = [self dateWithDayInterval:indexPath.row sinceDate:firstDayInYear];
   // NSDate *date = [NSDate date];
    //NSDate *dateForCell = [self nextDayFromDate:date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd-EEEE"];

    NSString *stringFromDate = [formatter stringFromDate:dateForCell];
    cell.textLabel.text = stringFromDate;
}
}

- (NSDate *)firstDayInYear:(NSInteger)year {
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:@"MM/dd/yyyy"];
firstDayInYear = [fmt dateFromString:[NSString stringWithFormat:@"01/01/%d", year]];
return firstDayInYear;
  }

  - (NSDate *)dateWithDayInterval:(NSInteger)dayInterval sinceDate:(NSDate *)referenceDate {
static NSInteger SECONDS_PER_DAY = 60 * 60 * 24;
return [NSDate dateWithTimeInterval:dayInterval * SECONDS_PER_DAY sinceDate:referenceDate];
  }
4

2 に答える 2

0

これは私がそれを達成できると思う新しい方法です

    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:16];
    [comps setMonth:6];
    [comps setYear:2013];
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *date = [gregorian dateFromComponents:comps];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEE MM"];
    NSString *day = [dateFormatter stringFromDate:date];

    [comps release];
    [gregorian release];

    NSLog(@"Weekday : %@", day);
于 2013-06-19T10:17:20.023 に答える