如何使用Objective C选择TableviewCell检查标记行值?

时间:2016-01-05 08:40:07

标签: ios objective-c xcode tableview

点击提交按钮后,我需要创建tableviewcell checkmark个选定的行title标签和detail标签值。

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

    if(cell == nil )
    {
        cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    if ([indexPath compare:self.lastIndexPath] == NSOrderedSame) 
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } 
    else 
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

// UITableView Delegate Method

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    cell = [tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"Section:%ld Row:%ld selected and its data is %@ %@",(long)indexPath.section,(long)indexPath.row,cell.sector_Label.text,cell.textLabel.text);
    selectedIndex = indexPath.row;
    [tableView reloadData];
}



- (IBAction)Submit_Click:(id)sender {

// Here I need to get tableview cell check mark selected cell label values.

}

2 个答案:

答案 0 :(得分:1)

检查一下:

-(IBAction)Submit_Click:(id)sender {

    // Here I need to get tableview cell check mark selected cell label values.
    if (selectedIndex >= 0) {
        NSIndexPath* indexPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        NSLog(@"detailTextLabel: %@", cell.detailTextLabel.text);
        NSLog(@"title: %@", cell.textLabel.text);
    }
}

答案 1 :(得分:0)

您可以使用indexPathsForSelectedRows

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

    cell = [tableView cellForRowAtIndexPath:indexPath];
    if(cell.isSelected)
    {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    else
    {
      [cell setSelected:YES animated:YES]
    }
}

-(IBAction)Submit_Click:(id)sender {

// Here I need to get tableview cell check mark selected cell label values.
 NSArray *indexes = [self.tableview indexPathsForSelectedRows];
 if (indexes.count > 0)
 {
   //do your stuff as you get selected row index array
 }
}