【发布时间】:2016-06-01 10:41:39
【问题描述】:
我想处理多行选择,即不超过 3 行,数据来自服务(URL)。下面是我尝试过的部分代码,但无法处理多行选择。你能不能花点时间来解决它。 TIA
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
static NSString *MyIdentifier = @"tableCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomCell *) currentObject;
}
}
}
NSDictionary *thisRow = [self.professionArr objectAtIndex:row];
if(_WSConstProfessionID !=nil && ![_WSConstProfessionID isEqual:@"0"] && ![_WSConstProfessionID isEqual:@""] && _WSConstProfessionSelectedIndex ==row ){
cell .accessoryType=UITableViewCellAccessoryCheckmark;
} else {
cell .accessoryType=UITableViewCellAccessoryNone;
}
cell.lblTitle.text = [thisRow objectForKey:_WSColumnProfessionName];
NSString *str=[thisRow objectForKey:_WSColumnProfessionID];
NSString *stra=_WSConstProfessionID;
if ([str isEqualToString:stra]) {
cell .accessoryType=UITableViewCellAccessoryCheckmark;
cell.highlighted=YES;
} else {
cell .accessoryType=UITableViewCellAccessoryNone;
}
return cell;
}
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSUInteger row = [indexPath row];
NSDictionary *thisRow=[self.professionArr objectAtIndex:row];
NSLog(@"%@",[thisRow description]);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
if(_WSConstProfessionID!=nil && ![_WSConstProfessionID isEqual:@"0"] && ![_WSConstProfessionID isEqual:@""] &&_WSConstProfessionSelectedIndex!=row){
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_WSConstProfessionSelectedIndex inSection:0]];
if (cell != nil){
cell .accessoryType=UITableViewCellAccessoryNone;
}
}
if( cell.accessoryType == UITableViewCellAccessoryCheckmark){
_WSConstProfessionID=[thisRow objectForKey:_WSColumnProfessionID];
_WSConstProfessionName=[thisRow objectForKey:_WSColumnProfessionName];
_WSConstProfessionSelectedIndex=row ;
} else {
_WSConstProfessionID=@"0";
_WSConstProfessionName=@"Select";
_WSConstProfessionSelectedIndex=-1 ;
}
[self.navigationController popViewControllerAnimated:YES];
}
【问题讨论】:
-
你能显示professionalArr值吗?并且您希望用户最多可以选择 3 行。
标签: ios objective-c iphone xcode uitableview