iphone - UITableView 复制所有行的第一个 NSArray 项目

标签 iphone objective-c cocoa-touch uikit

我的 NSMutableArray 和 UITableView 遇到了一个奇怪的问题。

数组中有 4 个项目,表格显示 4 行,但每行仅包含第一个数组项目,而不是第 1 行显示数组项目 1,第 2 行显示项目 2,等等...

这是我的代码:

我的可变数组 //在.h中

NSMutableArray *tableRows;

//以.m为单位

tableRows = [[NSMutableArray arrayWithObjects:@"For Business, For Pleasure",
                  @"A Great Decision",
                  @"Air Charter Your Honeymoon",
                  @"Time Flies", nil] retain];

我的表数据源和委托(delegate)有

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return (NSInteger)[tableRows count];
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"CELL_AIR";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if(cell == nil) {
        cell = [self makeTableCell:cellID identifier:indexPath];
    }
    return cell;
}

-(UITableViewCell *)makeTableCell:(NSString *)identifier identifier:(NSIndexPath *)indexPath {
    //frames
    CGRect cellFrame = CGRectMake(0, 10, 320, 50);
    CGRect lbl1Frame = CGRectMake(10, 0, 320, 25);
    CGRect lbl2Frame = CGRectMake(10, 20, 320, 20);


    UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:cellFrame reuseIdentifier:identifier];

    UILabel *lbl1 = [[UILabel alloc] initWithFrame:lbl1Frame];
    lbl1.tag=1;
    lbl1.font = [UIFont systemFontOfSize:19];
    lbl1.text = [tableRows  objectAtIndex:indexPath.row];


    UILabel *lbl2 = [[UILabel alloc] initWithFrame:lbl2Frame];
    lbl2.tag=2;
    lbl2.text = @"Tap to read more...";
    lbl2.font = [UIFont systemFontOfSize:12];

    [cell.contentView addSubview:lbl1];
    [cell.contentView addSubview:lbl2];

    return cell;
}

-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

任何帮助将不胜感激!

谢谢

C

最佳答案

这会导致错误:

-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
    return 1;
}

每个部分只有 1 行。因此,对于每个部分,索引路径再次从 0 开始,然后您只能获取数组中的第一项

我认为最好的方法应该是:

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

-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
    return (NSInteger)[tableRows count];
}

关于iphone - UITableView 复制所有行的第一个 NSArray 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3487349/

相关文章:

iphone - UITextField 使文本加粗?

objective-c - IOS:无法从 presentedViewController 访问 presentingViewController 的属性和方法

ios - 从静态方法更新 UI TableView

ios - iOS 中的存储使用情况,objective-c

iphone - Sharekit for Facebook(Graph API 版本)注销方法未完全注销或取消用户授权

ios - 如何在我的 ios 应用程序中为帮助页面添加此格式化文本

iphone - 如何将标签栏与类似 Springboard 的界面集成?

iphone - 为什么 UIWebView 不释放其所有内存?

ios - 有没有办法查看 iPhone 设备是否打开或关闭 "Keyboard Clicks"?

iphone - 内存管理