В моем приложении я хочу показать даты недели в UITableView. Я пытаюсь использовать код ниже, но он повторяет ту же дату во всех строках. Фактически, что я хочу показать, это календарные даты в 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];
}
это нормально, но я хочу, вт 18, ср 19, как это. –
Вот руководство по форматированию даты: http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns – Ron
Проблема @avi - это условие, которое вы поставили, целое, поэтому оно показывает только целочисленное значение EEEE. для названия дней недели, но показывается 0 1 2 3 4 5 6. –