views:

57

answers:

1

I search some Similar question like this question Need approach to show tables using segmented control?

the solution is using single tableview

But I think my problem is a little different

because the view will have a segmented control,has two selection: "DHCP" and "Manually"

When I pressed "DHCP",there will be a grouped table under the segmented controller

This tableview is uneditable ,only shows 3 items(IP address,mask,Router)in each rows

But if pressed "Manually",the tableview will become editable

Only can type like row 1"IP Address : 169.95.192.1",row 2 "Subnet mask 255.255.255.0"...

So my question is

<1>How to use segmented control to switch two different table ?

<2>How to create a editable tableview ?

great thanks for reading and reply this question.

A: 

right... well - you need to have a global BOOL eg BOOL isManual;

now in each of your UITableViewDatasource methods you need to check this bool:

- (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(isManual){
        // set cell content for manual
    }
    else{
        //set cell content for DCHP
    }

    return cell;
}

// this function allows you to set a table view cell as editable
// look up UITableView delegate methods for more :)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if(isManual){
        return YES;
    }
    else{
        return NO;
    }
}

And similar.

Then in your segmented control callback method you want to change isManual and reloadData on your table:

- (void)segmentedControlChanged:(id)selector {
    UISegmentedControl *control = selector;
    int selected = [control selectedSegmentIndex];

    if(selected == 0){
            isManual == NO;
        }
        else{
            isManual == YES;
        }
    [self.tableView reloadData];
}

Hope that helps somewhat - although its rather vague. :)

Thomas Clayson
thanks ,I think this logic is right,but I'm not yet to implement your code,I will try it ,and tell you is it work or not , only a little vague ,not big deal.
WebberLai
What if I use IB to create two tableview,than use if statement, if(selectedSegmentIndex == 0){view1.hidden=NO;view2.hidden=YES;} else{view1.hidden=YES;view2.hidden=NO;},will it be more easier ?
WebberLai
i don't know... you could try it. :) I don't use IB if I'm doing complicated things like this you see. Its much more methodical to do it programatically. :)
Thomas Clayson
I know ,I don't like IB too...I wish I can done every thing without IB...just using code !
WebberLai