selecting row

0 views
Skip to first unread message

harish kotagiri

unread,
Nov 11, 2009, 1:12:20 AM11/11/09
to iphonesdkd...@googlegroups.com
Hi friends,
I want select single row from each section in my table view.
for that iam written this code....

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   switch (section)
    {
       case 0:
           return [Price count];
           break;
        case 1:
           return [Size count];
            break;
       case 2:
           return [Style count];
           break;
        default:
            return 0;
            break;
   }
   
}

// Customize the appearance of table view cells.
- (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];
    }

    // Set up the cell...
   
    switch ((int)indexPath.section)
    {
        case 0:
            cell.textLabel.text = [[itemPriceType objectAtIndex:indexPath.row] objectForKey:@"TypeName"];
            break;
        case 1:
            cell.textLabel.text = [[itemSizeType objectAtIndex:indexPath.row] objectForKey:@"SizeName"];
            break;
        case 2:
            cell.textLabel.text = [[itemStyleType objectAtIndex:indexPath.row] objectForKey:@"StyleName"];
            break;
    }
   
    if(selected)
        if(indexPath.section==selectCell.section)
        {

    switch (selectCell.section)
    {
        case 0:
            if(indexPath.row==selectCell.row)
            {
                cell.imageView.image=[UIImage imageNamed:@"select.png"];
            }
            else {
                cell.imageView.image=[UIImage imageNamed:@"unselect.png"];
            }
            break;
        case 1:
            if(indexPath.row==selectCell.row)
            {    cell.imageView.image=[UIImage imageNamed:@"select.png"];
            }
            else {
                cell.imageView.image=[UIImage imageNamed:@"unselect.png"];
            }
            break;
        case 2:
            if(indexPath.row==selectCell.row)
            {   
                cell.imageView.image=[UIImage imageNamed:@"select.png"];
            }
            else {
                cell.imageView.image=[UIImage imageNamed:@"unselect.png"];
            }
            break;
           
    }
}
   
    cell.textLabel.font=[UIFont fontWithName:@"arial" size:15];
   
    return cell;
   
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if(section == 0)
        return @"a";
    else
        if(section == 1)
            return @"b";
    else
            return @"c";
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selected=TRUE;
    selectCell=[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
    [myTable reloadData];
}


in this code work well only for second section
my problem is when i am selecting the row from first section it is effecting the third section..
how can i modify the code for selecting one row from each section.....

Thanks in advance...

--
Thanks & Regards,

Harish Kotagiri

ankit thakur

unread,
Nov 11, 2009, 3:09:15 AM11/11/09
to iphonesdkd...@googlegroups.com
Hi Harish

Try to put the same no of count of objects in each section, and then try to select the first row of any section, and tell whether it is changing the cell image for all the 3 sections.
As the logic which you have written in cellForRowAtIndexPath is wrong to change the cell's image. try to change the comparison from row to indexpath

like instead of writing too many lines of code as
  switch (selectCell.section) 
    {
        case 0:
            if(indexPath.row==selectCell.
row)
            {
                cell.imageView.image=[UIImage imageNamed:@"select.png"];
            }
            else {
                cell.imageView.image=[UIImage imageNamed:@"unselect.png"];
            }
            break;
        case 1:
            if(indexPath.row==selectCell.
row)
            {    cell.imageView.image=[UIImage imageNamed:@"select.png"];
            }
            else {
                cell.imageView.image=[UIImage imageNamed:@"unselect.png"];
            }
            break;
        case 2:
            if(indexPath.row==selectCell.
row)
            {    
                cell.imageView.image=[UIImage imageNamed:@"select.png"];
            }
            else {
                cell.imageView.image=[UIImage imageNamed:@"unselect.png"];
            }
            break;
            
    }

just write 

if(indexPath == selectCell)
{    

     cell.imageView.image=[UIImage imageNamed:@"select.png"];
}
else {
     cell.imageView.image=[UIImage imageNamed:@"unselect.png"];
}
it will work the same

Thanks & Regards
ANKIT THAKUR
9962151771
Java/iPhone Developer
E-mail :  ankitt...@gmail.com

harish kotagiri

unread,
Nov 11, 2009, 3:18:57 AM11/11/09
to iphonesdkd...@googlegroups.com
Hi ANKIT,
Thanks for your response,
I am getting same problem when modifying the code also.

ankit thakur

unread,
Nov 11, 2009, 4:24:20 AM11/11/09
to iphonesdkd...@googlegroups.com
Hi Harish

It is working for me, in my sample code I have done it as

#pragma mark Table view methods


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 3;

}



// Customize the number of rows in the table view.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 3;

}

// Customize the appearance of table view cells.

- (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(selected)

{

if(indexPath == selectedIndex)

{    

cell.textLabel.text = @"selected";

}

else {

cell.textLabel.text=@"not selected";

}

}

else {

cell.textLabel.text = @"not selected";

}


// Configure the cell.


    return cell;

}

// Override to support row selection in the table view.

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

selected = YES;

selectedIndex = indexPath;

[tableView reloadData];


}

harish kotagiri

unread,
Nov 11, 2009, 4:34:36 AM11/11/09
to iphonesdkd...@googlegroups.com
Hi Ankit,
This code is well working for selecting single row in complete table view,
but i want select one row from each section in table view...

ankit thakur

unread,
Nov 11, 2009, 5:37:16 AM11/11/09
to iphonesdkd...@googlegroups.com
Hi Harish,
Got your point,
So do this way, it is working for the case you have told

#pragma mark Table view methods


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 3;

}



// Customize the number of rows in the table view.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 3;

}



// Customize the appearance of table view cells.

- (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(selected)

{

switch (indexPath.section) {

case 0:

if(indexPath == selectedIndex0)

{    

cell.textLabel.text = @"selected";

}

else {

cell.textLabel.text=@"not selected";

}

break;

case 1:

if(indexPath == selectedIndex1)

{    

cell.textLabel.text = @"selected";

}

else {

cell.textLabel.text=@"not selected";

}

break;

case 2:

if(indexPath == selectedIndex2)

{    

cell.textLabel.text = @"selected";

}

else {

cell.textLabel.text=@"not selected";

}

break;

default:

break;

}

}

else {

cell.textLabel.text = @"not selected";

}


// Configure the cell.


    return cell;

}





// Override to support row selection in the table view.

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

selected = YES;

switch (indexPath.section) {

case 0:

selectedIndex0 = indexPath;

case 1:

selectedIndex1 = indexPath;

case 2:

selectedIndex2 = indexPath;

break;

default:

break;

Reply all
Reply to author
Forward
0 new messages