2013-06-19 3 views
0

В моем приложении я хочу показать даты недели в 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]; 
    } 

ответ

2

Добавить значение indexPath.row на сегодняшний день, оно отображает новую дату в каждой ячейке.

NSMutableArray *dayCollectionArray=[[NSMutableArray alloc]init]; 
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"EEE, dd"]; 

for (int i = 0; i<7; i++) { 
    NSDate *nextDay = [NSDate dateWithTimeInterval:(i*24*60*60) sinceDate:[NSDate date]]; 

    NSString *day=[dateFormatter stringFromDate:nextDay]; 
    [dayCollectionArray addObject:day]; 

    NSLog(@"day=%d == =%@",i,day); 

} 

внутри cellForRowAtIndexPath: метод

cell.textLable.text = [dayCollectionArray objectAtIndex: indexPath.row];

Внутри цикла вы можете найти день taday и следующие 6 дней, рассчитать весь день и добавить в NSMutableArray, чем вы можете просто показать всю неделю в TableView.

+0

это нормально, но я хочу, вт 18, ср 19, как это. –

+0

Вот руководство по форматированию даты: http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns – Ron

+0

Проблема @avi - это условие, которое вы поставили, целое, поэтому оно показывает только целочисленное значение EEEE. для названия дней недели, но показывается 0 1 2 3 4 5 6. –

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); 
+0

Это будет печать дней, как вы хотите Tue 13 или Wed 19 – channi

+0

нет, это не потому, что формат nsstring равен% d –

+0

@avi также дал тот же ответ – channi

Смежные вопросы