【发布时间】:2015-10-11 20:25:53
【问题描述】:
这是我在 iOS 开发中的第二个项目,我正在尝试在 UITableView 对象中显示黑客马拉松的行程。我将活动列表及其相关时间存储在一个名为“events”的 NSString 变量中,然后尝试使用“cell.textLabel.text = schedule.events”显示它,它只显示了 events 变量中的第一个活动.然后我尝试增加行的高度,但这也没有解决我的问题。我接近这个错误吗?
我开始认为 events 应该是一个 NSArray 保存每个活动的字符串文字,但后来无法弄清楚如何将该数组中的每个字符串文字显示为三个部分的单独行:星期五、星期六和星期日.谁能指出我如何做到这一点的正确方向?
这是我的带有 UITableView 的 ViewController 的代码
#import <UIKit/UIKit.h>
#include "Weekend.h"
@interface ScheduleViewController : UIViewController <UITableViewDataSource,
UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tablesView;
@end
#import "ScheduleViewController.h"
@interface ScheduleViewController ()
@end
@implementation ScheduleViewController {
NSArray *allWeekendDays;
NSArray *allEvents;
}
@synthesize tablesView;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Set up schedule for friday
Weekend *friday = [[Weekend alloc] init];
friday.day = @"Friday, March 20th\n";
friday.events = @"9:00 PM Check-In\n"
"11:00 PM Opening Ceremony\n"
"11:59 PM Begin Hacks";
//Set up schedule for saturday
Weekend *saturday = [[Weekend alloc] init];
saturday.day = @"Saturday, March 21st";
saturday.events = @"2:00 AM Snack Time\n"
"8:00 AM Breakast\n"
"1:00 PM Lunch\n"
"7:30 PM Dinner\n"
"10:00 PM Nerf-Gun Wars";
//Set up schedule for sunday
Weekend *sunday = [[Weekend alloc] init];
sunday.day = @"Sunday, March 22nd";
sunday.events = @"2:00 AM Snack Time\n"
"6:30 AM Breaskfast\n"
"7:30 AM End Hacks\n"
"8:00 AM Expo 1\n"
"9:00 AM Expo 2\n"
"10:00 AM Closing Ceremony";
allWeekendDays = [NSArray arrayWithObjects:friday, saturday, sunday, nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark Table View Data Source Method
/*Ask about how to set style of table to grouped programmatically
- (id)initWithFrame:(CGRect)aRect style:(UITableViewStyle)style {
return self.tablesView = [[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped];
}
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//Defines how many sections in my table
return [allWeekendDays count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//Defines how many rows will be in each section
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Defines how each individual cell will loook like
static NSString *simpleTableIdentifier = @"WeekendCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
}
//Changed indexPath.row to indexPath.section
Weekend *schedule = [allWeekendDays objectAtIndex:indexPath.section];
cell.textLabel.text = schedule.events;
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//Define the header of each section to be the week day
Weekend *schedule = [allWeekendDays objectAtIndex:section];
return schedule.day;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100.0;
}
@end
【问题讨论】:
标签: ios objective-c iphone uitableview