1

UItableviewで、最初の4行をデフォルトでチェマークしたいのですが、どうすればこれを実現できますか?ここに私のコードがあります

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
[am.genar addObject:[ar objectAtIndex:indexPath.row]];
NSLog(@"array content%@",[ar objectAtIndex:indexPath.row]);   
if (thisCell.accessoryType == UITableViewCellAccessoryNone) 
{
    thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
    thisCell.accessoryType = UITableViewCellAccessoryNone;
}    
}
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath 
{
   return UITableViewCellAccessoryNone;
}

cellrowarindexpathメソッド

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.textLabel.text = [ar objectAtIndex:indexPath.row];        // Configure the cell...

if(indexPath.row<4)
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;
}

解決するのを手伝ってください。

4

1 に答える 1

4
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    if(indexPath.row < 4) //considering that you need FIRST four rows to be checkmarked by default
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

return cell;
}

質問が正しく理解できなかった場合はお知らせください。それに応じて修正を加えます。ハッピーコーディング:)

于 2012-09-18T05:52:10.723 に答える