作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 numberOfSections... 方法,如下所示:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
BOOL showContacts = self.selectedReminderMessageService != RMReminderMessageServiceTwitter ? YES : NO;
if (self.isEditing) {
if (showContacts) {
return 5;
} else {
return 4;
}
} else {
if (showContacts) {
return 4;
} else {
return 3;
}
}
}
我应该如何创建 cellForRowAtIndexPath... 方法?我是否必须像这样列出所有可能的配置:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BOOL showContacts = self.selectedReminderMessageService != RMReminderMessageServiceTwitter ? YES : NO;
NSInteger section = [indexPath section];
if (self.isEditing) {
if (showContacts) {
if (section == 0) {
// repeat to section 4 with else if's
}
} else {
if (section == 0) {
// repeat to section 3 else if's
}
}
} else {
if (showContacts) {
if (section == 0) {
// repeat to section 3 else if's
}
} else {
if (section == 0) {
// repeat to section 2 else if's
}
}
}
}
这可以用更有效的方式实现吗?
最佳答案
我遇到了类似的问题,最终创建了一个枚举器和一个给定 indexPath(或部分)的方法,返回它是什么部分。
这样,无论何时您需要在给定索引处找到您正在处理的单元格类型(例如,单元格的创建和选择),您只需询问该方法它是什么类型。
例子:
typedef enum {
SectionNone = 0,
SectionContacts,
SectionOptions,
} Section; // do a more appropriate name
- (Section)sectionForSection:(NSInteger)section {
// evaluate your state and return correct section
}
所以在你的 cellForRow 中......你可以去
Section sect = [self sectionForSection:indexPath.section];
switch (sect) {
case SectionContacts: {
// work with contact cell
break;
}
case SectionOptions: {
// work with options cell
break;
}
// etc
}
关于objective-c - iOS – cellForRow...IF 逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12204662/
我是一名优秀的程序员,十分优秀!