我是iPhone开发人员的新手,
在我的应用程序中,当我点击按钮弹出窗口时出现。
我想要做的是,当我选择popover中的任何一行时,该单元格应标有右箭头图像,如:
cell.accessoryType = UITableViewCellAccessoryCheckmark;
我希望获取所选行的文字。
Inshort 我想在我的popover中实现复选框,如果选择了5行,那么我想要获取的5行文本并将其存储在数组中。
这是我的代码:
-(void)btnClicked{
UIViewController* popoverContent = [[UIViewController alloc]init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(110, 0, 500, 4)];
UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 250, 665) style:UITableViewStylePlain];
[table setDelegate:(id<UITableViewDelegate>)self];
[table setDataSource:(id<UITableViewDataSource>)self];
[self.view addSubview:table];
[table release];
[popoverView addSubview:table];
popoverContent.view = popoverView;
popoverContent.contentSizeForViewInPopover = CGSizeMake(250, 600);
self.popoverController = [[UIPopoverController alloc]
initWithContentViewController:popoverContent];
[self.popoverController presentPopoverFromRect:CGRectMake(100,0, 535, 35)
inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
[popoverView release];
[popoverContent release];
}
在此之后我将我的数组传递给UITableView。
提前致谢!
答案 0 :(得分:1)
请在didSelectedRow方法中实现以下代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
NSMutableArray *cells = [[NSMutableArray alloc] init];
for(int index=0;index<indexPath.row;index++)
{
UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
[cells addObject:cell.textLabel.text];
}
}
答案 1 :(得分:0)
请找到我编辑过的新代码:
if(cells == nil)
cells = [[NSMutableArray alloc] init];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath]
{
if(indexPath.row < cells.count)
{
NSMutableDictionary *dict = [cells objectAtIndex:indexPath.row];
BOOL isChecked = [[dict objectForKey:"isChecked"] boolValue];
if(!isChecked)
{
[cells removeAllObjects];
for(int index=0;index<indexPath.row;index++)
{
UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
dict = [[NSMUtableDictionary alloc] init]
[dict setObject:@"true" forKey:@"isChecked"];
[dict setObject:cell.textLabel.text forKey:@"rowText"];
[cells addObject:dict];
[dict release];
}
}
else
{
UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];
[cell setAccessoryType:UITableViewCellAccessoryNone];
[cells removeObjectAtIndex:indexPath.row];
}
}
else
{
[cells removeAllObjects];
for(int index=0;index<indexPath.row;index++)
{
UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
dict = [[NSMUtableDictionary alloc] init]
[dict setObject:@"true" forKey:@"isChecked"];
[dict setObject:cell.textLabel.text forKey:@"rowText"];
[cells addObject:dict];
[dict release];
}
}
}
请注意,您的数组应该是全局的