hai,我想将复选框放在tableview的每一行。请提供任何示例代码,以便将复选框放在tableview单元格中
答案 0 :(得分:4)
你可以做这样的事情
- (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];
}
答案 1 :(得分:1)
使用UIButton并将其默认和所选图像设置为未选中,然后选中。
Here is the tutorial,在堆栈上有很多类似的问题,再次提出相同的问题,尝试搜索。