iPhone UITableView,如何设置默认行作为onViewLoad检查?

时间:2009-07-22 15:15:55

标签: iphone uitableview

我有一个UITableView正确填充数据。表视图位于另一个视图中。加载此父视图时,如何设置表视图中的第一行以显示复选标记?

@implementation PrefViewController 

@synthesize label, button, listData, lastIndexPath;

-(void) viewDidLoad {
    NSArray *array = [[NSArray alloc] initWithObjects:@"European",@"Spanish", nil];
    self.listData = array;

    // SET TABLEVIEW ROW 0 to CHECKED
    [array release];
    [super viewDidLoad];
}

编辑:我只希望在创建视图时检查第一行。应该只能选择第二组中的一行(具有复选标记)。这是我的完整cellForRowAtIndexPath,你能发现问题吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier";  
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CheckMarkCellIdentifier];

NSUInteger row = [indexPath row];
NSUInteger oldRow = [lastIndexPath row];

if(indexPath.section == 1)
{
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CheckMarkCellIdentifier] autorelease];
        if (indexPath.row == 0)
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }

    cell.text = [listData objectAtIndex:row];
    cell.accessoryType = (row == oldRow && lastIndexPath != nil) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
}
else
{
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CheckMarkCellIdentifier] autorelease];
    }

    cell.text = [aboutData objectAtIndex:row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

return cell;
}

2 个答案:

答案 0 :(得分:4)

您需要在Table View Controllers cellForRowAtIndexPath方法中实现此功能。

if (indexPath.row == 0)
cell.accessoryType = UITableViewCellAccessoryCheckmark;

答案 1 :(得分:0)

试试这个答案:

if (cell.accessoryType == UITableViewCellAccessoryNone) {
   cell.accessoryType = UITableViewCellAccessoryCheckmark;  
}
else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
   cell.accessoryType = UITableViewCellAccessoryNone;
}