gpt4 book ai didi

iOS 使用 UISegmentedControl 重新加载 UITableView

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:49:40 26 4
gpt4 key购买 nike

您好,我正在尝试根据两个不同的数组重新加载我的 TableView 。应加载哪个数组由导航栏中的段控件确定。目前它只会加载第一个数组,按下段控件时不会发生任何事情。下面是我的代码,非常感谢任何关于为什么这不起作用的帮助。我还检查了 Nib 中是否连接了我的 IBAction 分段器。

MessageViewController.h

#import <UIKit/UIKit.h>

@interface MessageViewController : UIViewController<UITableViewDelegate> {
IBOutlet UISegmentedControl *segmentControl;
IBOutlet UINavigationBar *navBar;
IBOutlet UITableView *tableView;
}

@property (retain, nonatomic) IBOutlet UISegmentedControl *segmentControl;
@property (retain, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *inbox;
@property (nonatomic, retain) NSMutableArray *sent;

@end

MessageViewController.m

#import "MessageViewController.h"

@interface MessageViewController () <UITableViewDelegate>

@end

@implementation MessageViewController
@synthesize segmentControl;
@synthesize inbox;
@synthesize sent;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.tabBarItem.title = NSLocalizedString(@"Messages", @"Messages");
self.tabBarItem.image = [UIImage imageNamed:@"mail_2_icon&32"];
}
return self;
}

- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.delegate = self;
self.inbox = [NSMutableArray arrayWithObjects:@"testing", @"test", @"another", nil];
self.sent = [NSMutableArray arrayWithObjects:@"test", @"another", @"testing", nil];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

#pragma mark Table view methods

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


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(segmentControl.selectedSegmentIndex == 0){
return [inbox count];
}else if(segmentControl.selectedSegmentIndex == 1){
return [sent count];
}else{
return [inbox count];
}
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if(segmentControl.selectedSegmentIndex == 0){
NSString *cellValue = [inbox objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
}else if(segmentControl.selectedSegmentIndex == 1){
NSString *cellValue = [sent objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
}else{
NSString *cellValue = [inbox objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
}
return cell;
}

-(IBAction)segmenter{
[tableView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

- (void)dealloc {
[inbox release];
[sent release];
[segmentControl release];
[segmentControl release];
[super dealloc];
}

- (void)viewDidUnload {
[self setSegmentControl:nil];
[segmentControl release];
segmentControl = nil;
[super viewDidUnload];
}

@end

最佳答案

不确定为什么它最后没有工作,我所做的只是删除三个类并使用上面的代码重做所有东西,一定是一路上被搞砸了,但它现在正在工作,我是一个快乐的露营者。也不需要委托(delegate)的东西,因为这些都是在 nib 中完成的,所以我的原始代码工作正常。

关于iOS 使用 UISegmentedControl 重新加载 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11906625/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com