【问题标题】:How to display long NSString in 1 row of a section in UITableView?如何在 UITableView 的某一部分的 1 行中显示长 NSString?
【发布时间】:2015-10-11 20:25:53
【问题描述】:

这是我在 iOS 开发中的第二个项目,我正在尝试在 UITableView 对象中显示黑客马拉松的行程。我将活动列表及其相关时间存储在一个名为“events”的 NSString 变量中,然后尝试使用“cell.textLabel.text = schedule.events”显示它,它只显示了 events 变量中的第一个活动.然后我尝试增加行的高度,但这也没有解决我的问题。我接近这个错误吗?

我开始认为 events 应该是一个 NSArray 保存每个活动的字符串文字,但后来无法弄清楚如何将该数组中的每个字符串文字显示为三个部分的单独行:星期五、星期六和星期日.谁能指出我如何做到这一点的正确方向?

Screenshot of iOS Simulator

这是我的带有 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


    【解决方案1】:

    最简单的解决方法是将cell.textLabel.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;在我看来DidLoad
    • 由于 UITableView 是 UIScrollView 的子视图,所以你的 table view 应该默认滚动
    • 感谢上面的代码,这正是我的第二个想法!它不允许我在 iOS 模拟器中滚动浏览我的表格
    • 您无法像在 OS X 应用程序中那样滚动。尝试拖动鼠标
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    相关资源
    最近更新 更多