我有一个UItableview和2个自定义单元格,两者都有当前日期的标签。然后我们可以调用A的一个自定义单元格在dayLabel下面有一个标签,然后自定义单元格B在dayLabel下面有两个标签。
然后我在1节中有7行。每天一排。它始终是有序的,因此当天是最重要的。而我现在要做的是让那些标记在星期六和星期日的细胞,是细胞类型A,其余的细胞是B细胞。
所以基本上无论什么日子,dayLabel星期六或星期日的单元格我想要单元格类型A,其余单元格类型B.这是我的tableview的代码。
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *daysOfTheWeek = @[@"Sunday",@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday"];
NSArray *mealsforAlbany = @[@"Breakfast", @"Lunch", @"Dinner"];
NSArray *mealsforUnion = @[@"Lunch", @"Dinner"];
self.mealsArray = ([currentUserAll[@"school"] isEqualToString:@"Union College (NY)"])? mealsforUnion : mealsforAlbany;
NSCalendar *cal = [NSCalendar currentCalendar];
NSInteger dayNumber = [cal component:NSCalendarUnitWeekday fromDate:[NSDate date]] - 1; // Sunday gives 0
//The following 3 lines create a new array starting with today's weekday
NSMutableArray *temp = [[daysOfTheWeek subarrayWithRange:NSMakeRange(dayNumber, 7- dayNumber)] mutableCopy];
[temp addObjectsFromArray:[daysOfTheWeek subarrayWithRange:NSMakeRange(0, 7 - temp.count)]];
self.weeksRotation = temp;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 7;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 7;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return self.weeksRotation[section];
// return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 75;
}
这里是cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger feedIndex = indexPath.row;
if (feedIndex < 7) { //set this a varibale and depending on if statements set its vaule
TwoMealsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TwoMealsCell" forIndexPath:indexPath];
//cell.textLabel.text = self.mealsArray[indexPath.row];
cell.backgroundColor = garnetColor;
cell.dayLabel.text = self.weeksRotation[indexPath.row];
[cell.firstMealBtn setTintColor:[UIColor whiteColor]]; //set this as a vaible for if stamtens
[cell.secondMealBtn setTintColor:[UIColor whiteColor]];
cell.dayLabel.textColor = [UIColor whiteColor];
[cell.firstMealBtn setTitle:@"Lunch" forState:UIControlStateNormal];
[cell.secondMealBtn setTitle:@"Dinner" forState:UIControlStateNormal];
cell.firstMealBtn.tag = indexPath.row;
cell.secondMealBtn.tag = indexPath.row;
[cell.firstMealBtn addTarget:self action:@selector(firstMealBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.secondMealBtn addTarget:self action:@selector(secondMealBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
return cell;
}
}
在这里,我将它全部设置为B型单元格,但我需要的是星期六和星期日单元格为A型和B的其余部分。
感谢您的帮助。
修改
例如,对于A学校,1号餐厅,我周一至周五,我想要两餐,然后周六和周日,我想1.但是学校A餐厅2,我周一 - 周五4餐,周六2餐和周日3饭菜。是每种情况下只有一堆if语句的唯一方法吗?
答案 0 :(得分:1)
您只需要根据self.weeksRotation [indexPath.row]的值将所需的单元格出列。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.weeksRotation[indexPath.row] isEqualToString:@"Saturday"] || [self.weeksRotation[indexPath.row] isEqualToString:@"Sunday"]) {
OneMealTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OneMealCell" forIndexPath:indexPath];
//set up the cell
return cell;
}else{
TwoMealsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TwoMealsCell" forIndexPath:indexPath];
cell.textLabel.text = self.mealsArray[indexPath.row];
cell.backgroundColor = garnetColor;
cell.dayLabel.text = self.weeksRotation[indexPath.row];
[cell.firstMealBtn setTintColor:[UIColor whiteColor]]; //set this as a vaible for if stamtens
[cell.secondMealBtn setTintColor:[UIColor whiteColor]];
cell.dayLabel.textColor = [UIColor whiteColor];
[cell.firstMealBtn setTitle:@"Lunch" forState:UIControlStateNormal];
[cell.secondMealBtn setTitle:@"Dinner" forState:UIControlStateNormal];
cell.firstMealBtn.tag = indexPath.row;
cell.secondMealBtn.tag = indexPath.row;
[cell.firstMealBtn addTarget:self action:@selector(firstMealBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.secondMealBtn addTarget:self action:@selector(secondMealBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
return cell;
}
}
修改强>
您可以通过多种方式完成任务,但考虑到您在多所学校设有多个餐厅,这将使逻辑变得复杂。一种方法是使用多个DaysOfTheWeek数组,每个不同的情况一个。它不是一个简单的数组,而是一个字典数组。这使得必须创建大量这些数组的大部分复杂性,但使cellForRowAtIndexPath中的代码更简单。像这样的东西,
NSArray *albany1 = @[@{@"day":@"Sunday", @"meals":@[@"Lunch", @"dinner"]}, @{@"day":@"Monday", @"meals":@[@"Breakfast",@"Lunch", @"dinner"]},@{@"day":@"Tuesday", @"meals":@[@"Breakfast",@"Lunch", @"dinner"]},@{@"day":@"WednesDay", @"meals":@[@"Breakfast",@"Lunch", @"dinner"]},@{@"day":@"Thursday", @"meals":@[@"Breakfast",@"Lunch", @"dinner"]},@{@"day":@"Friday", @"meals":@[@"Breakfast",@"Lunch", @"dinner"]},@{@"day":@"Saturday", @"meals":@[@"Lunch", @"dinner"]}];
NSArray *albany2 = ....
// etc. one array for each dining hall
您将使用从第一个控制器发送的任何信息来选择正确的数组,然后将该数组传递给那些重新排列数组的行,以使今天的工作日成为第一个元素(所以你会仍然只有一周的旋转阵列)。你需要尽可能多的不同细胞,因为你有不同数量的膳食,所以可能需要四个细胞,每天一餐,两餐,三餐或四餐。然后,您可以在cellForRowAtIndexPath中使用switch语句来选择要出列的单元格。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
int buttonNumber = [self.weeksRotation[indexPath.row][@"meals"] count];
switch (buttonNumber) {
case 1: {
OneMealTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OneMealCell" forIndexPath:indexPath];
// configure the cell
return cell;
}
case 2: {
TwoMealTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TwoMealCell" forIndexPath:indexPath];
// configure the cell
return cell;
}
case 3: {
ThreeMealTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThreeMealCell" forIndexPath:indexPath];
// configure the cell
return cell;
}
case 4: {
FourMealTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FourMealCell" forIndexPath:indexPath];
// configure the cell
return cell;
}
}
}