作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的应用程序中,我使用了 UItableview 组表。其中有两个部分,我想对其进行验证。
例如:在第 1 部分中有两行,如果用户选择第 1 行,则用户必须从第 2 部分中选择一些值,如果用户在第 1 部分中选择了第二行,则无需在第 2 部分中选择。
以下是我的代码:
- (void)viewDidLoad
{
NSArray *arr_data1 =[[NSArray alloc]initWithObjects:@"Yes",@"No",nil];
NSArray *arr_data2 =[[NSArray alloc] initWithObjects:@"Monotherapy",@"Adjunctive",nil];
NSDictionary *temp =[[NSDictionary alloc]
initWithObjectsAndKeys:arr_data1,@"",arr_data2,
@"Was it monotherapy or adjunctive",nil];
self.tableContents =temp;
self.arr_data =[[self.tableContents allKeys]
sortedArrayUsingSelector:@selector(compare:)];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.arr_data count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.arr_data objectAtIndex:section];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *listData =[self.tableContents objectForKey:
[self.arr_data objectAtIndex:section]];
return [listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:
[self.arr_data objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView
dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *listData =[self.tableContents objectForKey:
[self.arr_data objectAtIndex:[indexPath section]]];
NSUInteger row = [indexPath row];
NSLog(@"fdfdfdf=%d",[indexPath row]);
NSString *rowValue = [listData objectAtIndex:row];
if ([rowValue isEqualToString:@"Yes"])
{
NSString *message = [[NSString alloc] initWithFormat:@"%@",rowValue];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"You selected"
message:message delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
最佳答案
In - (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath ,
you can check the section you need by using the following code:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
{
//do as per you requirement
}
else if(indexPath.section == 1)
{
//do as per you requirement
}
}
关于iphone - 如何在 ios uitableview(组表)中执行验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13835518/
我是一名优秀的程序员,十分优秀!