[英]putting limitation for checkbox in UITableView Objective-C
我以編程方式創建具有不同部分和行的表格,在表格內創建復選框,將圖片作為框
我想對此復選框設置限制
你能幫我嗎
編輯:
我的問題是如何只選擇一個復選框-選擇的局限性
以及如何取消選擇一個框並刪除復選標記
這是復選框行的代碼
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
if (indexPath.section != 2) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.textLabel.text =contentForThisRow;
return cell;
}
else {
CheckBoxAbsenceTableViewCell *cell = (CheckBoxAbsenceTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"CheckBoxAbsenceTableViewCell"];
cell.imageView.image=[UIImage imageNamed:@"emptycheck-box.png"];
cell.checkBox.image = image;
if(indexPath.row == 0){
cell.absenceCode.text =@"Red";
}
else if (indexPath.row == 1){
cell.absenceCode.text =@"Green";
}else if(indexPath.row == 2){
cell.absenceCode.text =@"Blue";
}
return cell;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CheckBoxAbsenceTableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
selectedCell.checkBox.image =[UIImage imageNamed:@"checkmark.png"];
}
提前致謝!
從UI設計的角度來看,您應該使用cell.accessoryType和UITableViewCellAccessoryCheckmark附件。
您需要跟蹤數據模型中選定的(即選中的)indexPath。 讓我們稱之為self.idxPathSelected。
didSelectRowAtIndexPath會在選中的行中添加選中標記,並從先前選中的行中清除選中標記:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
if (self.idxPathSelected != nil)
{
cell = [tableView cellForRowAtIndexPath:self.idxPathSelected];
cell.accessoryType = UITableViewAccessoryNone;
}
self.idxPathSelected = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
}
在您的cellForRowAtIndexPath中,是這樣的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
if ((self.idxPathSelected != nil) && ([self.idxPathSelected compare:indexPath] == NSOrderedSame))
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
...
}
簡單的解決方案是將UIInteger _selectedIndex
實例var添加到視圖控制器。
在viewDidLoad中將其設置為-1(未選擇任何行)
然后,當用戶選擇一個單元格時,保存選定的索引並重新加載tableView:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_selectedIndex = indexPath.row;
[self.tableView reloadData];
}
更改cellForRowAtIndexPath
方法,使其僅選擇選定的單元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
if (indexPath.section != 2) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.textLabel.text =contentForThisRow;
return cell;
}
else {
CheckBoxAbsenceTableViewCell *cell = (CheckBoxAbsenceTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"CheckBoxAbsenceTableViewCell"];
if (indexPath.row == _selectedIndex) {
cell.checkBox.image =[UIImage imageNamed:@"checkmark.png"];
}
else {
cell.checkBox.image = [UIImage imageNamed:@"emptycheck-box.png"];;
}
if(indexPath.row == 0){
cell.absenceCode.text =@"Red";
}
else if (indexPath.row == 1){
cell.absenceCode.text =@"Green";
}else if(indexPath.row == 2){
cell.absenceCode.text =@"Blue";
}
return cell;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.