在我的应用程序中,我想在 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];
}