iphone - UITableView 复选标记和部分

标签 iphone xcode uitableview

我正在制作一个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/

相关文章:

iphone - Objective-C - 比 NSCaseInsensitiveSearch 更好的搜索?关键词? NSPredicate?

iOS 设计师不渲染 Storyboard

ios - 如何禁用 UIScrollView 的垂直滚动?

ios - 使用几种不同类型的单元格滚动 UITableView 不够平滑

ios - 如何在 iOS 中借助 ARKit 测量设备与人脸的距离?

ios - 确定数组中的最小值并在另一个数组中找到对应的字符串

iphone - Objective-C 显示来自 Web 服务的 HTML 文件

ios - 使用自动布局在元素之间添加 UI 元素

ios - 比较两个 NSDictionaries 并找出差异

ios - tableview 内的 UITextView 不根据内容调整大小