【发布时间】:2015-04-01 20:24:56
【问题描述】:
我有一个可变数组 (editedServiceParts),其中包含两个对象;我有一个包含大约 30 个单元格的表格视图。我想检查是否有任何表格视图单元格 (cell.textLabel.text) 出现在可变数组中;如果他们这样做,我想将 cell.accessory 设置为复选标记。
已更新这是我来自 -cellForRowAtIndexPath 的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// get the timeFormat
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *preferencesDict = [[userDefaults dictionaryForKey:@"preferencesDictionary"] mutableCopy];
int iTimeFormat = [[preferencesDict objectForKey:@"timeFormat"] intValue]; // set timeFormat
NSMutableArray *selectedServicesParts = [NSMutableArray new];
NSMutableArray *editedServiceParts = [NSMutableArray new];
NSString *service;
if(tableView.tag == kServicesTableView) { // services array
static NSString *CellIdentifier = @"apptServicesCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell
SingletonServicesArray *sharedServicesArray = [SingletonServicesArray sharedServicesArray]; // list of available services
[cell.textLabel setText:[sharedServicesArray.globalServicesArray objectAtIndex:indexPath.row]]; // move selected row to cell
// separate soServices.text into individual elements
NSString *cleanService;
if(soServices.text.length > 0) {
selectedServicesParts = [[soServices.text componentsSeparatedByString:@","] mutableCopy];
for(int x = 0; x < selectedServicesParts.count; x++) {
cleanService = [selectedServicesParts[x] stringByReplacingOccurrencesOfString:@"," withString:@""];
[editedServiceParts insertObject: cleanService atIndex: x];
}
}
// now, take the editedServicesParts (w/o commas) and mark cell if appropriate
for (int i = 0; i < editedServiceParts.count; i++) { if ([editedServiceParts containsObject:cell.textLabel.text]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
// Remove accessory mark of recycled cells
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
// for (int i = 0; i < editedServiceParts.count; i++) { // total number of rows in list of all available services
//
// for(service in editedServiceParts) {
// if([cell.textLabel.text isEqualToString: service]) {
// cell.accessoryType = UITableViewCellAccessoryCheckmark;
// break;
}
// }
// }
return cell;
}
所发生的只是找到了第一个比较,尽管应该找到另一个比较。我担心我这里有一个逻辑问题,而对于我的生活,我看不到它。非常感谢您的帮助!
标清
【问题讨论】:
-
为什么需要外循环,多次重复同一个任务?
-
您的内部和外部循环都在迭代editedServiceParts。您是否打算让内部循环遍历您的表格视图单元格?
-
cell永远不会改变,因此您会不断检查相同的单元格文本。 -
嗯...此代码在 -cellForRowAtIndexPath... 我可能应该提到...对不起...
-
你能用完整的
cellForRowAtIndexPath代码更新你的问题吗?
标签: objective-c uitableview ios8.1 fast-enumeration