我目前正在努力使我正在开发的应用程序的代码更高效、更容易阅读。基本上,这是在用户用两个手指(手势)点击之后,它从播放器名称的NSUserDefaults中检索以前保存的数组,并使用这些名称填充表视图上的6个文本框(标记为6-11)。如果没有现有的数组,它将使用默认的名称集。
我试图缩短长for/if语句的长度。任何简化此代码的想法都将不胜感激。
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
NSMutableArray *names = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"nameArray"]];
for (int i = 0; i <= 5; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
UITableViewCell *cell = [playerTable cellForRowAtIndexPath:indexPath];
for (UIView *view in cell.contentView.subviews) {
if ([view isKindOfClass:[UITextField class]]) {
UITextField *txtField = (UITextField *)view;
if (txtField.tag == 6) {
if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
txtField.text = [names objectAtIndex:0]; }
else {
txtField.text = @"Peter";
}
}
if (txtField.tag == 7) {
if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
txtField.text = [names objectAtIndex:1]; }
else {
txtField.text = @"Julia";
}
}
if (txtField.tag == 8) {
if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
txtField.text = [names objectAtIndex:2]; }
else {
txtField.text = @"Durgan";
}
}
if (txtField.tag == 9) {
if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
txtField.text = [names objectAtIndex:3]; }
else {
txtField.text = @"Bob";
}
}
if (txtField.tag == 10) {
if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
txtField.text = [names objectAtIndex:4]; }
else {
txtField.text = @"Iseland";
}
}
if (txtField.tag == 11) {
if([[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"]) {
txtField.text = [names objectAtIndex:5]; }
else {
txtField.text = @"Player";
}
}
}
}
}
[self saveNames];
}编辑:我在其他地方收到了一个答复,它大大缩短了篇幅,并使其更具可读性:
NSArray *defaultNames = @[@"Peter", @"Julia",...];
int offsetIndex = 6;
BOOl needCustomNames = [[NSUserDefaults standardUserDefaults] boolForKey:@"customNames"];
for (UIView *view in cell.contentView.subviews)
{
if ([view isKindOfClass:[UITextField class]])
{
UITextField *txtField = (UITextField *)view;
int index = [txtField tag]-offsetIndex;
if (txtField.tag >= 6 && txtField.tag <= 11)
{
if (needCustomNames)
txtField.text = [names objectAtIndex:index];
else
txtField.text = [defaultNames objectAtIndex:index];
}
}
}发布于 2014-07-10 11:37:59
我们还能做得更好。
首先,我们讨论的是一个UITableViewCell,您在其中添加了自定义视图--或者至少修改了现有视图(因此是标记),并添加了手势识别器。
最有意义的是用子类代替UITableViewCell。这将有助于清理您在这里发布的方法,以及清理cellForRowAtIndexPath:,我相信这会有点混乱。
然后,您将不再需要遍历所有content视图的子视图。相反,最好的情况是,单元格上只有一个文本字段,您可以直接访问它,它的标记上有switch,最坏的情况是,您可以创建一个与此方法相关的自定义IBCollection--一个文本字段数组--并迭代它(您知道它只包含与此方法相关的文本字段)。
int index = [txtField tag]-offsetIndex;
if (tag >= 6 && tag <= 11)
{
if (needCustomNames)
textField.text = [names objectAtIndex:index];
else
textField.text = [defaultNames objectAtIndex:index];
}这段代码令人困惑。
首先,tag在任何地方都没有声明。
第二,假设if应该检查textField.tag,您应该将int index行移动到if块中,或者更好的做法是重构if以检查刚刚基于textField.tag计算的index,以及if块中实际使用的值。目前,如果有人在不知不觉中更改了之前的offsetIndex 7行,那么这段代码可能会崩溃,这可能是一个很难理解的崩溃。
我认为还有更多的清理工作要做,但我想看看您是否有兴趣在重新编写代码以使用UITableViewCell子类的地方发布一个新的问题,这确实是最好的选择。
https://codereview.stackexchange.com/questions/56632
复制相似问题