Thursday, 8 March 2012

how to make tableview with custom switch

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if (section==0) {
        return array.count;
    }
    else{
        return 1;
    }
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    static NSString *CellIdentifier = @"Cell";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        //     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];     
    }
    if (indexPath.section==0) {
        UISwitch *switchObj = [[UISwitch alloc] initWithFrame:CGRectZero];
   
        [switchObj addTarget:self action:@selector(toggleValue:) forControlEvents:(UIControlEventValueChanged | UIControlEventTouchDragInside)];
        switchObj.tag=indexPath.row;
    //1 for the on
    //0 for the off
   
        if ([[appDel.storeValueArray objectAtIndex:indexPath.row] intValue]==0 && [appDel.storeValueArray count]>=1) {
            switchObj.on=NO;   
        }
        else if([appDel.storeValueArray count]>=1){
            switchObj.on=YES;       
        }

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryView = switchObj;
        cell.textLabel.text = [array objectAtIndex:indexPath.row];
    }
    else
    {
        cell.backgroundColor = [UIColor redColor];
        cell.textLabel.text = @"Remove Contacts";
        cell.textLabel.textColor = [UIColor whiteColor];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section==1) {
        contactsView *contact = [[contactsView alloc]initWithNibName:@"contactsView" bundle:nil];
        [self.navigationController pushViewController:contact animated:YES];
        [contact release];
    }
}   

No comments:

Post a Comment