创建自定义 UITableView [英] Creating a Customized UITableView

查看:27
本文介绍了创建自定义 UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照

I'm trying to create an UITableView as per in these images One tableview can have multiple sections and its titles. A Tableview can have number of sections and number of rows. But, in every section only one row can selectable. And, later i can display in my UIAlertView with selected row as message and section as title. How can i achieve this?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GroupCell"];

    if (cell == nil)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:@"GroupCell"];
    }

    if (indexPath.row == self.selectedCell)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.selected = YES;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selected = NO;
    }

    cell.textLabel.text = @"mytext"
    cell.textLabel.textColor = [UIColor blackColor];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.selectedCell = indexPath.row;

    [tableView reloadData]; // here i have to make the condition..
}

Any idea on this? Or at-least to display an UITableView like below image too is enough..

解决方案

For row to be selected from each section you have to keep an array of selectedCell And for time deselected all the cells you have to manage one more array that is isFirsttimeSelection And keep a boolArray for rowCollapse.

Interface or header file implementation..

NSArray *sectionArray;
NSMutableDictionary *dictionary;

NSMutableArray *boolArray;
NSMutableArray *selectedCell;
NSMutableArray *isFirsttimeSelection;

have a look at initialization of arrays and dictionary to be used.

dictionary=[[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSArray arrayWithObjects:@"Cat",@"Dog",@"Lion",@"Tiger",@"Elephant",nil],@"Animals",[NSArray arrayWithObjects:@"Swallow",@"Parrot",@"Eagle",@"Owl",nil],@"Birds",[NSArray arrayWithObjects:@"Banana",@"Mango",@"Grapes", nil],@"Fruits",nil];

sectionArray=[[NSArray alloc] initWithArray:[dictionary allKeys]];

boolArray=[[NSMutableArray alloc] init];
selectedCell=[[NSMutableArray alloc] init];
isFirsttimeSelection=[[NSMutableArray alloc] init];

for (int i=0;i<sectionArray.count;i++) {
    [boolArray addObject:[NSNumber numberWithBool:NO]];
    [isFirsttimeSelection addObject:[NSNumber numberWithInt:NO]];
    [selectedCell addObject:[NSNumber numberWithInt:0]];
}

tableView datasources and delegates to be used as:---

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [sectionArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([boolArray[section] boolValue]) {
        return [[dictionary valueForKey:[sectionArray objectAtIndex:section]] count];
    }
    return 0;
}

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

    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.accessoryType=((indexPath.row == [selectedCell[indexPath.section] intValue]) && ([isFirsttimeSelection[indexPath.section] intValue]==1))?UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text=[[dictionary valueForKey:[sectionArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}

Header view for section

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIButton *headerView=[UIButton buttonWithType:UIButtonTypeCustom];
    [headerView setTitle:[sectionArray objectAtIndex:section] forState:UIControlStateNormal];
    headerView.tag=section;
    headerView.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [headerView setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
    [headerView.titleLabel setFont:[UIFont boldSystemFontOfSize:16.0f]];
    [headerView setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [headerView setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [headerView.titleLabel setShadowOffset:CGSizeMake(0, -1)];

    [headerView addTarget:self action:@selector(sectionTouched:) forControlEvents:UIControlEventTouchUpInside];

    return  headerView;
}

Action on header view clicked

- (void)sectionTouched:(UIButton *)sender
{
    [boolArray replaceObjectAtIndex:sender.tag withObject:[NSNumber numberWithBool:([boolArray[sender.tag] boolValue])?NO:YES]];

    [_tblView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag] withRowAnimation:([boolArray[sender.tag] boolValue])?UITableViewRowAnimationTop:UITableViewRowAnimationBottom];
}

And at last did select

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
     selectedCell[indexPath.section] = [NSString stringWithFormat:@"%i",indexPath.row];
    isFirsttimeSelection[indexPath.section]=[NSNumber numberWithBool:YES];
    [tableView reloadData];
}

Download it.

这篇关于创建自定义 UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆