我对设置表视图单元配件感到困惑。
我在表格中修复了两个部分
我想要的是如下......
我试过以下代码。但我发现indexpath.row& indexpath.section是readonly属性。
// Override to support row selection in the table view.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tblProfileView deselectRowAtIndexPath:indexPath animated:YES];
int i,max;
UITableViewCell *x; NSIndexPath *tt;
for(i=0;i<[tblProfileView numberOfRowsInSection:0];i++)
{
tt.row=i; tt.section=0;
x=[tblProfileView cellForRowAtIndexPath:];
[x setAccessoryType:UITableViewCellAccessoryNone];
}
for(i=0;i<[tblProfileView numberOfRowsInSection:1];i++)
{
tt.row=i; tt.section=1;
x=[tblProfileView cellForRowAtIndexPath:tt];
[x setAccessoryType:UITableViewCellAccessoryNone];
}
x=[tblProfileView cellForRowAtIndexPath:indexPath];
[x setAccessoryType:UITableViewCellAccessoryCheckmark];
// Navigation logic may go here -- for example, create and push another view controller.
// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
// [self.navigationController pushViewController:anotherViewController animated:YES];
// [anotherViewController release];
}
答案 0 :(得分:49)
我会跟踪应该检查的数据并更改tableView中的单元格:didSelectRowAtIndexPath:并更新tableView中检查的数据:cellForRowAtIndexPath:像这样:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// do usual stuff here including getting the cell
// determine the data from the IndexPath.row
if (data == self.checkedData)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// determine the selected data from the IndexPath.row
if (data != self.checkedData) {
self.checkedData = data;
}
[tableView reloadData];
}
答案 1 :(得分:16)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
if (newCell.accessoryType == UITableViewCellAccessoryNone) {
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
}else {
newCell.accessoryType = UITableViewCellAccessoryNone;
}
}
您还需要删除cellForRowAtIndexPath
上的复选标记附件if ([selectedOptionsArray indexOfObject:cell.textLabel.text] != NSNotFound) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
答案 2 :(得分:14)
声明一个名为prev
的int变量并实现此方法: -
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =[tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType==UITableViewCellAccessoryNone)
{
cell.accessoryType=UITableViewCellAccessoryCheckmark;
if (prev!=indexPath.row) {
cell=[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:prev inSection:0]];
cell.accessoryType=UITableViewCellAccessoryNone;
prev=indexPath.row;
}
}
else{
cell.accessoryType=UITableViewCellAccessoryNone;
}
}
答案 3 :(得分:6)
仅在选定行中为选中标记实施逻辑的最短且最简单的方法.....
1. 创建NSIndexPath的全局对象..
selectedIndexPathForCheckMark= [[NSIndexPath alloc] init];
2. 在didSelectRowAtIndexPath ..
//将选定的indexPath分配给声明的对象
selectedIndexPathForCheckMark = indexPath;
[tableView reloadData];
3. cellForRowAtIndexPath中的 ..
if ([selectedIndexPathForCheckMark isEqual:indexPath]) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}else {
//setAccessoryType None if not get the selected indexPath
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
答案 4 :(得分:1)
以下是我如何使用静态单元格并绕过cellForRow
创建一个var,用于存储&#34;已检查&#34;的位置。细胞
NSInteger _checkedCell;
然后只需实施willDisplayCell
和didSelectRow
这样的方法。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (_checkedCell == indexPath.row) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
_checkedCell = indexPath.row;
[self.tableView reloadData];
}
答案 5 :(得分:1)
我知道这个问题已经很老了,但是这里有基于Blackberry响应的Swift版本(顺便提一下我已投票)
import UIKit
class PlacesViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource {
var placesArray = NSMutableArray()
var previousCheckedIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
self.placesArray.addObject("Tandil")
self.placesArray.addObject("Balcarce")
self.placesArray.addObject("Mar del Plata")
self.tableView.rowHeight = UITableViewAutomaticDimension
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.placesArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = self.placesArray.objectAtIndex(indexPath.row) as? String
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if (indexPath.row != previousCheckedIndex) {
var cell: UITableViewCell = self.tableView.cellForRowAtIndexPath(indexPath)!
if (cell.accessoryType == UITableViewCellAccessoryType.None) {
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
if (previousCheckedIndex != indexPath.row) {
cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: previousCheckedIndex, inSection: 0))!
cell.accessoryType = UITableViewCellAccessoryType.None
previousCheckedIndex = indexPath.row
}
} else {
cell.accessoryType = UITableViewCellAccessoryType.None
}
tableView.reloadData()
}
}
}
答案 6 :(得分:0)
在Swift 2.0中使用自定义accessoryView uitableviewcell with swift
1º创建Globar var IndexPath
var indexSelected = NSIndexPath()
2º在didSelectRowAtIndexPath
中func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
indexSelected = indexPath
TB_Lojas.reloadData()
}
3°in cellForRowAtIndexPath:
if SelectedIndexPath == indexPath {
let img = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
img.image = UIImage(named: "check")
cell.accessoryView = img
}else{
let img = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
img.image = UIImage(named: "uncheck")
cell.accessoryView = img
}
}
答案 7 :(得分:0)
考虑到您只想维持一个选择,如果您有自定义UITableViewCell
,我建议您使用以下内容。
在UITableViewController
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
//...
let data = dataArray[indexPath.row]
if data.isSelected {
tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: .None)
}
//...
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let data = dataArray[indexPath.row]
data.isSelected = true
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.setSelected(true, animated: true)
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let data = dataArray[indexPath.row]
data.isSelected = false
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.setSelected(false, animated: true)
}
在自定义UITableViewCell
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
self.accessoryType = .None
if selected {
self.accessoryType = .Checkmark
}
}
只要未启用allowsMultipleSelection
,当选择新单元格时,它将自动处理取消选择其他单元格。如果allowMultipleSelection
已启用,那么它也可以正常工作,除非您只需再次点击取消选择。
答案 8 :(得分:0)
BlackBerry的回答在Swift 3. UITableView中使用标准单元格,选择0或1个单元格。
private var previousSelection = NSIndexPath()
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let cell = tableView.cellForRow(at: indexPath)!
if cell.accessoryType == .none {
cell.accessoryType = .checkmark
selection = indexPath
if previousSelection == IndexPath() {
previousSelection = indexPath
} else if previousSelection != indexPath {
let previousCell = tableView.cellForRow(at: previousSelection)
previousCell?.accessoryType = .none
previousSelection = indexPath
}
} else if cell.accessoryType == .checkmark {
cell.accessoryType = .none
selection = IndexPath()
}
}