Iphone 如何在tableview单元格中放置复选框?
hai,我想将复选框放置在tableview的每一行中。请提供任何示例代码,用于将复选框放置在tableview单元格中使用UIButton,并将其默认和选定图像设置为未选中,然后选中。Iphone 如何在tableview单元格中放置复选框?,iphone,Iphone,hai,我想将复选框放置在tableview的每一行中。请提供任何示例代码,用于将复选框放置在tableview单元格中使用UIButton,并将其默认和选定图像设置为未选中,然后选中。 ,堆栈中有许多与此相关的类似问题,在再次放置相同的问题之前,请尝试搜索它。您可以执行类似的操作 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
,堆栈中有许多与此相关的类似问题,在再次放置相同的问题之前,请尝试搜索它。您可以执行类似的操作
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIButton * btnShare=[[UIButton alloc] initWithFrame:CGRectMake(270,0,32,32)];
[btnShare setImage:[UIImage imageNamed:@"check-box.png"] forState:UIControlStateNormal];
[btnShare setImage:[UIImage imageNamed:@"check-box-selected.png"] forState:UIControlStateSelected];
[btnShare addTarget:self action:@selector(accessoryButtonTapped:withEvent:) forControlEvents: UIControlEventTouchUpInside];
cell.accessoryView = btnShare;
[btnShare release];
}
BOOL isShared = ([arrayOfSelectedIndex indexOfObject:[NSNumber numberWithInt:indexPath.row]] != NSNotFound);
UIButton* btnShare1 = (UIButton*)cell.accessoryView;
btnShare1.selected = isShared;
[btnShare1 setNeedsDisplay];
return cell;
}
其中arrayOfSelectedIndex是一个包含所有选定索引的数组
有更多的方法来帮助行的多重选择,但我相信上面的代码片段可以让您了解如何在单元格中获取复选框
希望有帮助。干杯
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
[tableView reloadData];
}
- (void) accessoryButtonTapped: (UIControl *) button withEvent: (UIEvent *) event{
NSIndexPath * indexPath = [neighbourTableView indexPathForRowAtPoint: [[[event touchesForView: button] anyObject] locationInView: neighbourTableView]];
if ( indexPath == nil )
return;
[arrayOfSelectedIndex removeAllObjects];
[arrayOfSelectedIndex addObject:[NSNumber numberWithInt:indexPath.row]];
[self tableView:neighbourTableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[arrayOfSelectedIndex removeAllObjects];
[arrayOfSelectedIndex addObject:[NSNumber numberWithInt:indexPath.row]];
[tableView reloadData];
}
这段代码非常有用,但我们在选择器方法中编写的内容。@venkat阅读Inder@RahulSharma非常感谢你这么大的努力,这对我帮助很大!!嘿,我有一个小问题,你能告诉我什么是我真正停留在这里的“邻里视图”吗?Well neighbourTableView只是我们希望显示选中标记按钮的tableview的IBOutlet。IBUITableView*邻里关系视图;您可以简单地使用tableView作为接收参数中的参数。我还在编辑答案,意思是你问了10个问题,没有接受任何答案…@venkat。这样做是对那些帮助过你的人的奖励,也为未来的用户提供了一个路标,在许多人中,具体的答案对解决给定的问题最有帮助。@venkat,还要注意——StackOverflow是一个高质量问题和答案的存储库,而不是一个让别人为你做工作的地方。请阅读更多关于我们社区希望看到的问题的详细信息。谢谢