Ios 扩展UITableView单元格不工作
我正在尝试重新创建手风琴桌面视图,其中一个单元格在点击时展开,以显示另一个隐藏的单元格,再次点击时收缩。此tableview是另一个视图中的子视图。我真的被困在这个问题上了,我真的不知道出了什么问题,但是输出是空白的 非常感谢任何帮助和启发 标题Ios 扩展UITableView单元格不工作,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在尝试重新创建手风琴桌面视图,其中一个单元格在点击时展开,以显示另一个隐藏的单元格,再次点击时收缩。此tableview是另一个视图中的子视图。我真的被困在这个问题上了,我真的不知道出了什么问题,但是输出是空白的 非常感谢任何帮助和启发 标题 #import <UIKit/UIKit.h> @interface CardViewController : UIViewController{ NSTimer *stopTimer; NSDate *startDate
#import <UIKit/UIKit.h>
@interface CardViewController : UIViewController{
NSTimer *stopTimer;
NSDate *startDate;
BOOL running;
NSTimeInterval secondsAlreadyRun;
NSArray *topItems;
NSMutableArray *subItems; // array of arrays
int currentExpandedIndex;
}
@property (strong, nonatomic) IBOutlet UILabel *timerLabel;
@property (strong, nonatomic) UIButton *timerPressed;
@property (strong, retain) UITableView *tableView;
- (IBAction)timerPressed:(id)sender;
- (IBAction)stopPressed:(id)sender;
- (void) updateTimer;
@end
#导入
@接口CardViewController:UIViewController{
NSTimer*停止计时器;
NSDate*起始日期;
布尔跑;
NSTimeInterval secondsAlreadyRun;
NSArray*顶级项目;
NSMUTABLEARRY*子项;//数组的数组
int currentExpandedIndex;
}
@属性(强,非原子)IBUILabel*timerLabel;
@属性(强,非原子)UIButton*timerPressed;
@属性(强,保留)UITableView*tableView;
-(iAction)timerPressed:(id)发送方;
-(iAction)已停止:(id)发送方;
-(void)updateTimer;
@结束
实施
#import "CardViewController.h"
#define NUM_TOP_ITEMS 2
#define NUM_SUBITEMS 2
@interface CardViewController (){
NSTimeInterval _lastTimerInterval;
}
@end
@implementation CardViewController
@synthesize timerLabel;
#pragma mark - main view
- (void)viewDidLoad
{
[super viewDidLoad];
running = false;
topItems = [[NSArray alloc] initWithArray:[self topLevelItems]];
subItems = [NSMutableArray new];
currentExpandedIndex = -1;
for (int i = 0; i < [topItems count]; i++) {
[subItems addObject:[self subItems]];
}
}
#pragma mark - Data generators
- (NSArray *)topLevelItems {
NSMutableArray *items = [NSMutableArray array];
for (int i = 0; i < NUM_TOP_ITEMS; i++) {
[items addObject:[NSString stringWithFormat:@"Item %d", i + 1]];
}
return items;
}
- (NSArray *)subItems {
NSMutableArray *items = [NSMutableArray array];
int numItems = arc4random() % NUM_SUBITEMS + 2;
for (int i = 0; i < numItems; i++) {
[items addObject:[NSString stringWithFormat:@"SubItem %d", i + 1]];
}
return items;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [topItems count] + ((currentExpandedIndex > -1) ? [[subItems objectAtIndex:currentExpandedIndex] count] : 0);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ParentCellIdentifier = @"ParentCell";
static NSString *ChildCellIdentifier = @"ChildCell";
BOOL isChild =
currentExpandedIndex > -1
&& indexPath.row > currentExpandedIndex
&& indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];
UITableViewCell *cell;
if (isChild) {
cell = [tableView dequeueReusableCellWithIdentifier:ChildCellIdentifier];
}
else {
cell = [tableView dequeueReusableCellWithIdentifier:ParentCellIdentifier];
}
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ParentCellIdentifier];
}
if (isChild) {
cell.detailTextLabel.text = [[subItems objectAtIndex:currentExpandedIndex] objectAtIndex:indexPath.row - currentExpandedIndex - 1];
}
else {
int topIndex = (currentExpandedIndex > -1 && indexPath.row > currentExpandedIndex)
? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count]
: indexPath.row;
cell.textLabel.text = [topItems objectAtIndex:topIndex];
cell.detailTextLabel.text = @"";
}
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
BOOL isChild =
currentExpandedIndex > -1
&& indexPath.row > currentExpandedIndex
&& indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];
if (isChild) {
NSLog(@"A child was tapped, do what you will with it");
return;
}
[self.tableView beginUpdates];
if (currentExpandedIndex == indexPath.row) {
[self collapseSubItemsAtIndex:currentExpandedIndex];
currentExpandedIndex = -1;
}
else {
BOOL shouldCollapse = currentExpandedIndex > -1;
if (shouldCollapse) {
[self collapseSubItemsAtIndex:currentExpandedIndex];
}
currentExpandedIndex = (shouldCollapse && indexPath.row > currentExpandedIndex) ? indexPath.row - [[subItems objectAtIndex:currentExpandedIndex] count] : indexPath.row;
[self expandItemAtIndex:currentExpandedIndex];
}
[self.tableView endUpdates];
}
- (void)expandItemAtIndex:(int)index {
NSMutableArray *indexPaths = [NSMutableArray new];
NSArray *currentSubItems = [subItems objectAtIndex:index];
int insertPos = index + 1;
for (int i = 0; i < [currentSubItems count]; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:insertPos++ inSection:0]];
}
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
- (void)collapseSubItemsAtIndex:(int)index {
NSMutableArray *indexPaths = [NSMutableArray new];
for (int i = index + 1; i <= index + [[subItems objectAtIndex:index] count]; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}
- (IBAction)timerPressed:(id)sender {
if (!running){
running = true;
// [sender setTitle:@"Pause" forState:UIControlStateNormal];
if (stopTimer == nil){
stopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
target:self
selector:@selector(updateTimer)
userInfo:nil
repeats:YES];
startDate = [NSDate date];
}
}
else {
running = false;
[stopTimer invalidate];
stopTimer = nil;
secondsAlreadyRun += [[ NSDate date] timeIntervalSinceDate:startDate];
}
}
- (IBAction)stopPressed:(id)sender {
[stopTimer invalidate];
self.timerPressed.userInteractionEnabled = NO;
self.timerLabel.enabled = NO;
int timeDifference = (600 - _lastTimerInterval);
int sec = timeDifference % 60;
int min = floor((timeDifference/60));
NSString *finalTime = [NSString stringWithFormat:@"%d" @":" @"%d", min, sec];
NSLog(@"%@",finalTime);
//slight uncorrected issue, if press timer twice will restart.
}
-(void)updateTimer{
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
timeInterval += secondsAlreadyRun;
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString *timeString=[dateFormatter stringFromDate:timerDate];
timerLabel.text = timeString;
_lastTimerInterval = timeInterval;
}
@end
#导入“CardViewController.h”
#定义NUM_TOP_项目2
#定义NUM_子项2
@接口CardViewController(){
nTimeInterval\u lastTimerInterval;
}
@结束
@CardViewController的实现
@合成时间标签;
#pragma标记-主视图
-(无效)viewDidLoad
{
[超级视图下载];
运行=错误;
topItems=[[NSArray alloc]initWithArray:[self-topLevelItems]];
子项=[NSMutableArray new];
currentExpandedIndex=-1;
对于(int i=0;i<[topItems count];i++){
[subItems addObject:[self subItems]];
}
}
#pragma标记-数据生成器
-(NSArray*)顶级项目{
NSMutableArray*项=[NSMutableArray];
对于(int i=0;i-1)?[[subItems对象索引:currentExpandedIndex]计数]:0;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*ParentCellIdentifier=@“ParentCell”;
静态NSString*ChildCellIdentifier=@“ChildCell”;
布尔是个孩子=
currentExpandedIndex>-1
&&indexPath.row>currentExpandedIndex
&&indexPath.row-1&&indexPath.row>currentExpandedIndex)
?indexath.row-[[subItems objectAtIndex:currentExpandedIndex]计数]
:indexath.row;
cell.textLabel.text=[topItems对象索引:topIndex];
cell.detailTextLabel.text=@”;
}
返回单元;
}
#pragma标记-表视图委托
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath{
布尔是个孩子=
currentExpandedIndex>-1
&&indexPath.row>currentExpandedIndex
&&indexath.row-1;
如果(应该崩溃){
[自折叠Subitemsatindex:currentExpandedIndex];
}
currentExpandedIndex=(shouldCollapse&&indexath.row>currentExpandedIndex)?indexath.row-[[subItems objectAtIndex:currentExpandedIndex]count]:indexath.row;
[自扩展项索引:currentExpandedIndex];
}
[self.tableView endUpdates];
}
-(void)expandItemAtIndex:(int)索引{
NSMUTABLEARRY*INDExpath=[NSMUTABLEARRY new];
NSArray*currentSubItems=[子项对象索引:索引];
int insertPos=索引+1;
对于(int i=0;i<[currentSubItems count];i++){
[indexPaths addObject:[NSIndexPath indexPathForRow:insertPos++片段:0];
}
[self.tableView insertRowsAtIndexPaths:indexPaths with rownanimation:uitableview rownanimationfade];
[self.tableView ScrollToRowatineXpath:[NSIndexPath indexPathForRow:索引位置:0]atScrollPosition:UITableViewScrollPositionTop动画:是];
}
-(无效)collapseSubItemsAtIndex:(int)索引{
NSMUTABLEARRY*INDExpath=[NSMUTABLEARRY new];
对于(int i=index+1;i虽然与代码的执行无关,但在分析代码时,我发现在处理表时有一个名为currentExpandedIndex
的int
值非常混乱。我建议将其重命名为currentExpandedRow
。并用NSInteger替换int,我真的不喜欢e混合了Obj-C和C(除非是for循环)。哇哇哇哇!请不要像return[topItems count]+((currentExpandedIndex>-1)?[[subItems objectAtIndex:currentExpandedIndex]count]:0]这样写行。
只需像3个nsInteger和一个普通的if-else块那样使用即可。这真的很难看。