gpt4 book ai didi

iOS:如何在tableview中响应显示参数?

转载 作者:行者123 更新时间:2023-11-28 21:16:56 26 4
gpt4 key购买 nike

我正在尝试在 UIViewController 中设置一个 UITableView。我正在使用 Storyboard。但是在模拟器上运行时,tableview 不显示任何内容。这是我在 Xcode 中尝试过的:

1) 在 UIView 上拖动一个 TableView 。

2) 连接它的导出(dataSourcedelegate)。

3) 我从服务器得到这样的响应:

[{"email_id":"adas@faga.gs","id":66,"mobile_no":"1236547895","name":"asad","relation":"dsfs"},{"email_id":"raj@ghj.com","id":67,"mobile_no":"5632145412","name":"raj","relation":"xyz"}]

4)现在我想做什么:

在我要显示的第一个单元格中,姓名:asad 手机:1236547895 电子邮件:adas@faga.gs 关系:dsfs

在第二个单元格中,name:raj mobile:5632145412 email:raj@ghj.com relation:xyz

但我不知道该怎么做。请任何人解决我的问题。帮助将是可观的。

我确实喜欢下面的方式,但它不起作用。

a) 使用 Objective-C 类模板向项目添加一个新文件。将其命名为 EmergencyContactCell 并使其成为 UITableViewCell 的子类。

b) 然后在 EmergencyContactCell.h

@interface EmergencyContactCell : UITableViewCell

@property (nonatomic, weak) IBOutlet UILabel *name;
@property (nonatomic, weak) IBOutlet UILabel *email;
@property (nonatomic, weak) IBOutlet UILabel *mobile;
@property (nonatomic, weak) IBOutlet UILabel *relation;

c) 然后是 MainStoryboard.storyboard,选择原型(prototype)单元并在 Identity Inspector 中将其类更改为 EmergencyContactCell

d) 然后连接所有 socket

e) 我正在使用 AFNetworking 从服务器获取响应,当我收到响应后,我喜欢以下方式:

NSArray *ResponseArray = [NSJSONSerialization JSONObjectWithData: responseObject options: kNilOptions error: nil];

if (ResponseArray.count >0)
{
menuItems = [ResponseArray mutableCopy];
[yourTableviewname reloadData];
}

f) 并将其显示在单元格上,

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [menuItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"EmergencyContactCell";

//static NSString *simpleTableIdentifier = @"cell";
EmergencyContactCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];



// NSDictionary *content = [menuItems objectAtIndex:indexPath.row];
// cell.textLabel.text = [NSString stringWithFormat:@"Name:%@, Mobile:%@, Email:%@, Relation:%@",content[@"name"],content[@"mobile_no"],content[@"email_id"],content[@"relation"]];
//


return cell;
}

我最后不知道该怎么办??

最佳答案

第一步

No need of this

NSArray *ResponseArray = [NSJSONSerialization JSONObjectWithData: responseObject options: kNilOptions error: nil];

NSMutableArray *finalArray = [NSMutableArray array];
for (NSDictionary *temp in ResponseArray) {
[finalArray addObject:temp[@"name"]];
}
NSLog(@"%@",finalArray);

if(finalArray != nil && finalArray.count > 0){

menuItems=[NSArray arrayWithArray:finalArray];
NSLog(@"menu items: %@",menuItems);


}
else{
NSLog(@"zero values from server");
}

只要喜欢

 NSArray *ResponseArray = [NSJSONSerialization JSONObjectWithData: responseObject options: kNilOptions error: nil];

if (ResponseArray.count >0)
{
menuItems = [ResponseArray mutableCopy];
[yourTableviewname reloadData];
}

第二步

继续回答

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSDictionary *content = [menuItems objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"Name:%@, Mobile:%@, Email:%@, Relation:%@",content[@"name"],content[@"mobile_no"],content[@"email_id"],content[@"relation"]];



return cell;
}

更新的答案

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"EmergencyContactCell";


EmergencyContactCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


NSDictionary *content = [menuItems objectAtIndex:indexPath.row];

cell.name.text = [NSString StringWithFormat:@"Name:%@",content[@"name"]];
cell.email.text = [NSString StringWithFormat:@"email:%@",content[@"email"]];
cell.mobile.text = [NSString StringWithFormat:@"mobile:%@",content[@"mobile"]];
cell.relation.text = [NSString StringWithFormat:@"relation:%@",content[@"relation"]];


return cell;
}

关于iOS:如何在tableview中响应显示参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41014668/

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