简体   繁体   中英

How to extract cell value/text from UITableView?

For some reasons, I have to extract a cell's text or value from a UITableView. I have the following table method:

- (void)loadCollection_Information
{
    _collection_InformationCellData = [[NSMutableArray alloc] init];
    NSMutableArray *cells_1 = [[NSMutableArray alloc] init];
    NSDictionary *cellContainer_1_1 = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"Brett Whiteley", @"Author", @"", @"", @"", @"", nil] forKeys:[NSArray arrayWithObjects:@"Text", @"Detail Text", @"Image", @"Text Color", @"Detail Text Color", @"Accessory", nil]];
    [cells_1 addObject:cellContainer_1_1];
    NSDictionary *cellContainer_1_2 = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"1976", @"Year", @"", @"", @"", @"", nil]
                            forKeys:[NSArray arrayWithObjects:@"Text", @"Detail Text", @"Image", @"Text Color", @"Detail Text Color", @"Accessory", nil]];
    [cells_1 addObject:cellContainer_1_2];
    NSDictionary *cellContainer_1_3 = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"Canvas, Oil", @"Materials", @"", @"", @"", @"", nil] forKeys:[NSArray arrayWithObjects:@"Text", @"Detail Text", @"Image", @"Text Color", @"Detail Text Color", @"Accessory", nil]];
    [cells_1 addObject:cellContainer_1_3];
    NSDictionary *cellContainer_1_4 = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"Social Comment & Hard Edged Abstraction", @"Style", @"", @"", @"", @"", nil]
                            forKeys:[NSArray arrayWithObjects:@"Text", @"Detail Text", @"Image", @"Text Color", @"Detail Text Color", @"Accessory", nil]];
    [cells_1 addObject:cellContainer_1_4];
    NSDictionary *cellContainer_1_5 = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"182.0 high * 200.0 wide cm", @"Size", @"", @"", @"", @"", nil]
                            forKeys:[NSArray arrayWithObjects:@"Text", @"Detail Text", @"Image", @"Text Color", @"Detail Text Color", @"Accessory", nil]];
    [cells_1 addObject:cellContainer_1_5];
    NSDictionary *sectionContainer_1 = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:@"Collection Information", cells_1, @"", nil]
                            forKeys:[NSArray arrayWithObjects:@"Title", @"Cells", @"Footer Title", nil]];
    [_collection_InformationCellData addObject:sectionContainer_1];

    _collection_InformationSelectedRow = 0;
    _collection_InformationSelectedSection = 0;
    _collection_InformationShowHeader = YES;
    [_collection_Information setEditing:NO];
    [_collection_Information reloadData];
}

and didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (tableView == _collection_Information) {
    if (indexPath.section == 0 && indexPath.row == 0) {
        UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"Brett_WhiteleyViewController"];
        [self.navigationController pushViewController:controller animated:YES];
    }
    if (indexPath.section == 0 && indexPath.row == 1) {
        UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"Year_1960s_70sViewController"];
        [self.navigationController pushViewController:controller animated:YES];
    }
    if (indexPath.section == 0 && indexPath.row == 2) {
        UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"Canvas_and_OilViewController"];
        [self.navigationController pushViewController:controller animated:YES];
    }
    if (indexPath.section == 0 && indexPath.row == 3) {
        UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"SC_and_HE_AbstractionViewController"];
        [self.navigationController pushViewController:controller animated:YES];
    }
}

//Get the Dictionary Object at index 0, to check out the object.
NSLog(@"%@",[_collection_InformationCellData objectAtIndex:0]);

//Store the Objects from dictionary in temp Array.
NSArray *temp = [[_collection_InformationCellData objectAtIndex:indexPath.section] valueForKey:@"Cells"];

//Get the object based on Row Selection in UITableView.
NSLog(@"%@",[[temp objectAtIndex: indexPath.row] valueForKey:@"Text"]);
}

I need to extract the NSStrings such as: Brett Whiteley, 1976, Canvas, Oil, Social Comment & Hard Edged Abstraction separately, and put it into follow code in another method:

-(NSString *)maxmsg
{

NSString *msg;
int maxint=-1;
RecommendationData *obj = [RecommendationData getInstance];

if(obj.author<=5 && obj.year<=5 && obj.material<=5 && obj.style<=5)
{
msg=@"nothing";
}

else if( obj.author >= obj.year && obj.author >= obj.material && obj.author >= obj.style)
{
    maxint=obj.author;

    //the code for placing the value 
    obj.maxValue= (place value here);

    msg=@"It seems that you are interested in the author of this collection.";
}
else if(obj.year >= obj.author  && obj.year >= obj.material && obj.year >= obj.style)
{
    maxint=obj.year;

    //the code for placing the value 
    obj.maxValue= (place value here);

msg=@"It seems that you are interested in this period of the collection.";
}
else
{
msg=@"equal";
}

return msg;
}

Can anyone tell me how to do it?

To receive "Brett Whiteley" from another method you can call

_collection_InformationCellData[ CellContainerIndex ][ @"Cells" ][ 0 ][ "Text" ]

but you need to save somewhere/to know the correct CellContainerIndex. May be it is just 0 in your case?

Here is the way to perform it :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Get the Dictionary Object at index 0, to check out the object.
    NSLog(@"%@",[_collection_InformationCellData objectAtIndex:0]);

    //Store the Objects from dictionary in temp Array.
    NSArray *temp = [[_collection_InformationCellData objectAtIndex:indexPath.section] valueForKey:@"Cells"];

    //Get the object based on Row Selection in UITableView.
    NSLog(@"%@",[[temp objectAtIndex: indexPath.row] valueForKey:@"Text"]);
}

You need to access the Array using Index and getting the values using key from that object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM