`
jsntghf
  • 浏览: 2477708 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

可伸缩的表视图(二)

    博客分类:
  • iOS
阅读更多

以前,写了一篇文章:可伸缩的表视图,实现的是类似QQ的可折叠形式的表视图,这篇文章提供了另一种解决方案。

 

下面只列出表视图的相关代码,其他代码请参考附件。

 

 

RootNavigationController.h

 

#import <UIKit/UIKit.h>

@interface RootNavigationController : UINavigationController {
    
}

@end

#import "QQSectionHeaderView.h"

@interface RootViewController : UITableViewController<QQSectionHeaderViewDelegate> {	
	NSMutableArray *lists;
}

@end

 

RootNavigationController.m

 

#import "RootNavigationController.h"
#import "QQList.h"

@implementation RootNavigationController

@end

#define HEADER_HEIGHT 35

@implementation RootViewController

- (void) loadQQData {
	for (int i = 0; i < 10; i++) {
		QQList *list = [[[QQList alloc] init] autorelease];
		list.m_nID = i;
		list.m_strName = @"QQGroupName";
		list.m_arrayPersons = [[[NSMutableArray alloc] init] autorelease];
		list.opened = YES; 
		for (int j = 0; j < arc4random() % 20; j++) {
			QQPerson *person = [[[QQPerson alloc] init] autorelease];
			person.m_nQQNumber = 10000 + abs(arc4random() % 100000000);
			person.m_nListID = i; 
			person.m_bIsOnline = arc4random() % 10 < 5 ? YES : NO;
			person.m_strNickName = @"[Eric]";	
			person.m_strRemarkName = arc4random() % 10 < 5 ? @"张三": @"李四";
			person.m_strLongNickInfo = @"http://2015.iteye.com"; 
			person.m_strHeadImageURLString = nil; 
			
			[list.m_arrayPersons addObject:person];
		}
		[lists addObject:list];
	}
}

- (void)loadView {
	[super loadView];
	lists = [[[NSMutableArray alloc] init] retain];	
	[self performSelector:@selector(loadQQData)];
}

- (void)viewDidLoad {
    [super viewDidLoad];
	[self setTitle:@"QQTableView"];
	self.tableView.delegate = self;
	self.tableView.dataSource = self;
}

- (void)dealloc {
	[lists release];
    [super dealloc];
}

#pragma mark -
#pragma mark Table view data source

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

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	QQList *persons = [lists objectAtIndex:section];
	if ([persons opened]) {
		return [persons.m_arrayPersons count]; 
	} else {
		return 0;
	}
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
	QQList *persons = [lists objectAtIndex:section];
	int onlineCount = 0;
	for (QQPerson *person in persons.m_arrayPersons) {
		if ([person isOnline]) {
			onlineCount++;
		}
	}
	NSString *headString = [NSString stringWithFormat:@"%@ [%d/%d] ", persons.m_strName, onlineCount, [persons.m_arrayPersons count]];
	
	QQSectionHeaderView *sectionHeadView = [[QQSectionHeaderView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.tableView.bounds.size.width, HEADER_HEIGHT) title:headString 
                                    section:section opened:persons.opened delegate:self];
	return [sectionHeadView autorelease];
}

- (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];
    }
	QQList *persons = [lists objectAtIndex:indexPath.section];
    QQPerson *person = [persons.m_arrayPersons objectAtIndex:indexPath.row];
	if ([person isOnline]) {
		[cell textLabel].text = person.m_strNickName;
	} else {
		[cell textLabel].text =[NSString stringWithFormat:@"%@ (Offline)", person.m_strNickName];
    }
	[cell textLabel].font = [UIFont boldSystemFontOfSize:14.0];
    
	[cell detailTextLabel].text = person.m_strLongNickInfo;
	[cell setImage:[UIImage imageNamed:@"headicon.jpg"]];

    return cell;
}

- (void)sectionHeaderView:(QQSectionHeaderView *)sectionHeaderView sectionClosed:(NSInteger)section
{
	QQList *persons = [lists objectAtIndex:section];
	persons.opened = !persons.opened;

	NSInteger countOfRowsToDelete = [self.tableView numberOfRowsInSection:section];
    if (countOfRowsToDelete > 0) {
        persons.indexPaths = [[NSMutableArray alloc] init];
        for (NSInteger i = 0; i < countOfRowsToDelete; i++) {
            [persons.indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:section]];
        }
        [self.tableView deleteRowsAtIndexPaths:persons.indexPaths withRowAnimation:UITableViewRowAnimationTop];
    }
}

- (void)sectionHeaderView:(QQSectionHeaderView *)sectionHeaderView sectionOpened:(NSInteger)section
{
	QQList *persons = [lists objectAtIndex:section];
	persons.opened = !persons.opened;
	
	if(persons.indexPaths) {
		[self.tableView insertRowsAtIndexPaths:persons.indexPaths withRowAnimation:UITableViewRowAnimationBottom];
	}
	persons.indexPaths = nil;
}

@end

 

示例图:


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics