我正在制作一个UITableView
,它允许选择单元格(类似于语言“TableView\”,但只有一次),但复选标记附件不起作用。
if (tableView == self.numberTableView) {
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.text = [arryNumber objectAtIndex:indexPath.row];
if ([cell.textLabel.text isEqualToString:@"0"])
{
if (inumberfont == 0) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
if ([cell.textLabel.text isEqualToString:@"1"])
{
if (inumberfont == 1) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
if ([cell.textLabel.text isEqualToString:@"2"])
{
if (inumberfont == 2) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
if ([cell.textLabel.text isEqualToString:@"3"])
{
if (inumberfont == 3) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
if ([cell.textLabel.text isEqualToString:@"4"])
{
if (inumberfont == 4) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
有人可以帮忙吗?
最佳答案
我重写了你的代码。
if (tableView == self.numberTableView) {
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.text = [NSString stringWithFormat:@"%i", indexPath.row];
if (inumberfont == indexPath.row) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
上面的代码与您的代码执行完全相同的操作,但是您的代码无法正常工作。在下面的代码中,我修复了我发现的错误。我还添加了一些缺失的代码。如果您以前有过它,但没有包含它,那也没关系;我只是想确保它没有丢失。
if (tableView == self.numberTableView) {
static NSString *CellIdentifier = @"NumberCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [NSString stringWithFormat:@"%i", indexPath.row];
if (inumberfont == indexPath.row) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
我不确定你所说的“复选标记附件不起作用”是什么意思,但请尝试根据我的建议改进你的代码,如果它仍然不起作用,请发表评论,我会看看我能做什么做进一步帮助您。
编辑:找出问题所在。请尝试以下操作:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
inumberfont = indexPath.row;
[tableView reloadData];
}
关于iphone - UITableView 复选标记和部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5280038/