私のアプリでは、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];
}