2009-07-22 73 views
0

我有一個UITableView正確填充數據。表格視圖在另一個視圖內。當加載此父視圖時,如何設置表視圖中的第一行以勾選?iPhone UITableView,如何將默認行設置爲選中onViewLoad?

@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; 
} 

回答

4

你需要在你的表視圖控制器cellForRowAtIndexPath方法中實現這個。

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

嘗試這樣的回答:

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