gpt4 book ai didi

objective-c - iOS – cellForRow...IF 逻辑

转载 作者:行者123 更新时间:2023-11-29 11:09:26 24 4
gpt4 key购买 nike

我有一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com