Ios 如何将UITableView节索引用作NSInteger?
我想将my的Ios 如何将UITableView节索引用作NSInteger?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我想将my的kJTCalendarDaySelected设置为等于可见的索引.section。如果索引为0,我希望kJTCalendarDaySelected设置为1(月1日),如果索引为1,我希望设置为2(月2日),依此类推 当我尝试这样做时: -(BOOL)whatSectionsAreVisible { NSArray *visibleRowIndexes = [self.agendaTable indexPathsForVisibleRows]; for (NSIndex
kJTCalendarDaySelected
设置为等于可见的索引.section
。如果索引为0,我希望kJTCalendarDaySelected
设置为1(月1日),如果索引为1,我希望设置为2(月2日),依此类推
当我尝试这样做时:
-(BOOL)whatSectionsAreVisible {
NSArray *visibleRowIndexes = [self.agendaTable indexPathsForVisibleRows];
for (NSIndexPath *index in visibleRowIndexes) {
self.daySection = index.section;
// Update views
[[NSNotificationCenter defaultCenter] postNotificationName:@"kJTCalendarDaySelected" object:self.daySection];
// Store currentDateSelected
[self.calendar setCurrentDateSelected:self.daySection];
NSLog(@"The visible section has an index of: %ld", (long)index.section);
if (index.section == 0) {
NSLog(@"index.row is indeed 0");
return YES;
}
}
return NO;
}
我得到以下错误:
ARC不允许将非目标c指针类型“NSInteger”(又名“long”)隐式转换为“id”
如何将index.section的id转换为可用于此目的的NSInteger?您需要传递参数类型id。 假设你能用这种方法
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInteger:self.daySection],@"sectionIndex", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"kJTCalendarDaySelected" object:dict];
希望这能帮助您……您可以使用下面的代码删除错误
[[NSNotificationCenter defaultCenter] postNotificationName:@"kJTCalendarDaySelected" object:[NSNumber numberWithInteger:self.daySection]];
NSString *strDate = [NSString stringWithFormat:@"%@", [NSNumber numberWithInteger:section]];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"MM"];
[formatter setDateStyle:NSDateFormatterMediumStyle];
NSDate *selecteDate = [[formatter standaloneMonthSymbols] objectAtIndex:[formatter dateFromString:strDate]];
NSLog(@"%@",selectedDate);
此处选择的日期将以月份名称的形式出现,如果您的indexath.section为“0”,则它将显示“一月”,以便您可以在日历中设置月份名称。使用
NSNumber*indexNumber=[NSNumber numberwhithinteger:self.dayssection]
并在通知对象中传递indexNumber
。您可以通过NSInteger daySectionIndex=[indexNumber integerValue]
将其转换为整数。回答标题中的问题:节索引是NSInteger
,不是指向对象的指针。postNotification
方法需要一个指针,显然您的setCurrentDateSelected
方法也需要一个指针。您的问题中不清楚的是通知观察者和setCurrentDateSelected
方法期望的对象类型……或者您可以使用现代装箱语法:…object:@(self.daySection)