Ios 如何在UITableView中在节的一行中显示长NSString?

Ios 如何在UITableView中在节的一行中显示长NSString?,ios,objective-c,iphone,uitableview,Ios,Objective C,Iphone,Uitableview,这是我在iOS开发中的第二个项目,我试图在UITableView对象中显示hackathon的行程。我正在将活动列表及其关联时间存储在一个名为“events”的NSString变量中,然后尝试使用“cell.textLabel.text=schedule.events”来显示它,它只显示events变量中的第一个活动。然后我试着增加行的高度,但这也没有解决我的问题。我是不是走错路了 我开始认为事件应该是一个包含每个活动的字符串文本的NSArray,但后来却不知道如何将该数组中的每个字符串文本显示

这是我在iOS开发中的第二个项目,我试图在UITableView对象中显示hackathon的行程。我正在将活动列表及其关联时间存储在一个名为“events”的NSString变量中,然后尝试使用“cell.textLabel.text=schedule.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
#导入
#包括“Weekend.h”
@接口ScheduleViewController:UIViewController
@属性(强,非原子)IBUITableView*TableView;
@结束
#导入“ScheduleViewController.h”
@接口ScheduleViewController()
@结束
@实现ScheduleViewController{
NSArray*所有周末;
NSArray*所有事件;
}
@综合观点;
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
//安排星期五的日程
周末*周五=[[周末分配]初始];
friday.day=@“3月20日星期五\n”;
friday.events=@“晚上9:00入住\n”
“晚上11:00开幕式\n”
“晚上11:59开始黑客”;
//安排周六的日程
周末*周六=[[Weekend alloc]init];
星期六.day=@“3月21日星期六”;
saturday.events=@“凌晨2:00小吃时间\n”
“上午8:00早餐\n”
“下午1:00午餐\n”
“晚上7:30晚餐\n”
“晚上10:00 Nerf枪战”;
//安排周日的日程
周末*周日=[[Weekend alloc]init];
sunday.day=@“3月22日,星期日”;
sunday.events=@“凌晨2:00小吃时间\n”
“上午6:30早餐\n”
“上午7:30结束破解\n”
“上午8:00世博会1\n”
“上午9:00世博会2\n”
“上午10:00闭幕式”;
allWeekendDays=[NSArray arrayWithObjects:周五、周六、周日,无];
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
#pragma标记表视图数据源方法
/*询问如何以编程方式将表的样式设置为“分组”
-(id)initWithFrame:(CGRect)aRect样式:(UITableViewStyle)样式{
返回self.tableView=[[UITableViewController alloc]initWithStyle:UITableViewStyleGrouped];
}
*/
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
//定义我的表中有多少节
返回[allWeekendDays计数];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
//定义每个节中的行数
返回1;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
//定义每个单元格的外观
静态NSString*simpleTableIdentifier=@“WeekendCell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:simpleTableIdentifier];
}
//将indexPath.row更改为indexPath.section
Weeken*schedule=[allWeekendDays对象索引:indexath.section];
cell.textlab.text=schedule.events;
返回单元;
}
-(NSString*)表格视图:(UITableView*)表格视图标题标题标题部分:(NSInteger)部分{
//将每个部分的标题定义为周日
周末*日程=[allWeekendDays对象索引:节];
返回时间表。天;
}
-(CGFloat)tableView:(UITableView*)表视图行高度索引路径:(NSIndexPath*)索引路径{
返回100.0;
}
@结束

最简单的解决方案是将
cell.textlab.numberOfLines
设置为0; 这样你就得到了一个自动成长的UILabel


但在我看来,我会在每个事件中使用一个单元格,而不是一天。您已经对各个日期进行了分组,这样就可以很好地工作了

为此,请将事件用作包含多个字符串的数组。 以下是一些小的代码更改:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //Defines how many rows will be in each section
    return [[allWeekendDays[section] events] count];
}

- (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[indexPath.row];

    return cell;
}

...
//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"];

是否需要添加更多代码以启用滚动?我将self.tableView.bounces=YES;在我的视图中,DidLoadas UITableView是UIScrollView的一个子视图,您的表视图应该按默认值滚动谢谢您提供上述代码,这正是我第二次思考时的想法!它不允许我在我的表格中滚动,尽管在iOS模拟器中,你不能像在OS X应用程序中那样滚动。试着拖动你的鼠标