我正在制作一个提醒应用程序,我需要显示事件名称,事件图像和事件时间。所有这些都是从diff数组获取值但问题是如果第一个数组的内容是2并且如果我指定 返回[timeArray count]; ,则应用程序崩溃,那么第二个是
这是我的代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// return [timeArray count];
// return [imagesArray count];
return [nameArrayP2 count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
cell.timeLabel.text = [timeArray objectAtIndex:indexPath.row];
cell.eventNameLbl.text =[nameArrayP2 objectAtIndex:indexPath.row];
UIImage *image=[UIImage imageNamed:[imagesArray objectAtIndex:indexPath.row]];
cell.imgView.image =image;
return cell;
}
6月6日更新了代码:
NSArray *timeA = [[NSUserDefaults standardUserDefaults]objectForKey:@"TimeArray"];
timeArray =[[NSMutableArray alloc]init];
[timeArray addObjectsFromArray:timeA];
NSLog(@"%@",timeArray);
NSArray *nameA = [[NSUserDefaults standardUserDefaults]objectForKey:@"nameArrayP2"];
nameArrayP2 =[[NSMutableArray alloc]init];
[nameArrayP2 addObjectsFromArray:nameA];
NSLog(@"%@",nameArrayP2);
NSArray *imageA =[[NSUserDefaults standardUserDefaults]objectForKey:@"images"];
imagesArray =[[NSMutableArray alloc]init];
[imagesArray addObjectsFromArray:imageA];
NSLog(@"abhi%@",nameArrayP2);
dataMutableArray = [[NSMutableArray alloc]init];
NSDictionary *dataDict = [[NSDictionary alloc]initWithObjectsAndKeys:nameArrayP2,@"eventTitle", timeArray,@"eventTime",imagesArray, @"eventImage", nil];
NSDictionary *dataDict1 = [[NSDictionary alloc]initWithObjectsAndKeys:nameArrayP2,@"eventTitle", timeArray,@"eventTime",imagesArray, @"eventImage", nil];
//dataMutableArray =[NSMutableArray arrayWithArray:[dataDict allValues]];
[dataMutableArray addObject:dataDict]; [dataMutableArray addObject:dataDict1];
// [dataMutableArray addObject:dataDict];
NSLog(@"All Data=%@",dataMutableArray);
答案 0 :(得分:0)
您可以为活动创建一个对象,您可以在其中拥有名称,图像,时间等属性..,
在重新加载tableView之前,从数组列表中创建一个对象,并将此事件保存为数组广告中的对象,并对tableView使用相同的内容。
答案 1 :(得分:0)
你应该有一个自定义对象,应该使用不同的dataArray初始化,然后在tableview数据源方法中使用自定义对象的数组
[BasicAuthentication]
public class YourController : ApiController{.......}
答案 2 :(得分:0)
您只需创建一个词典数组即可。 如果要稍后操作数组以添加或删除事件,请获取NSMutableArray的属性。
NSMutableArray *dataMutableArray = [[NSMutableArray alloc]init];
您可以创建数据字典并将其添加到数组中。像这样的东西。
NSDictionary *dataDict = [[NSDictionary alloc]initWithObjectsAndKeys:@"I am event title",@"eventTitle", @"I am image",@"eventImage",@"I am event time", @"eventTime", nil];
[dataMutableArray addObject:dataDict];
您还可以创建一个包含事件数据的模型类,如
@property (strong, nonatomic) NSString *eventName;
@property (strong, nonatomic) NSString *eventTime;
@property (strong, nonatomic) NSString *eventDate;
//Inside your view controller
EventData *eventObject = [[EventData alloc]init];
eventObject.eventTime = @"I am event time";
eventObject.eventImage = @"I am image";
eventObject.eventTitle = @"I am event title";
[dataMutableArray addObject:eventObject];
下面是你的tableview数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataMutableArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
//You can use any of the one approach
//1)
NSDictionary *dataDict = [dataMutableArray objectAtIndex:indexPath.row];
cell.timeLabel.text = [dataDict valueForKey:@"eventTime"];
cell.eventNameLbl.text =[dataDict valueForKey:@"eventTitle"];
UIImage *image=[UIImage imageNamed:[dataDict valueForKey:@"eventImage"]];
cell.imgView.image =image;
return cell;
//2)
EventData *eventObject = [dataMutableArray objectAtIndex:indexPath.row];
cell.timeLabel.text = eventObject.eventTime;
cell.eventNameLbl.text = eventObject.eventTitle;
UIImage *image=[UIImage imageNamed:eventObject.eventImage];
cell.imgView.image =image;
return cell;
}
答案 3 :(得分:0)
您应该遵循这种方式
拿一个NSObject类说EventBO
然后取2 NSString
和1 NSDate
(或根据您的要求采取其他类型)属性(eventName,eventImage,eventTime)。
注意:将图像名称保留在图像属性
#import <Foundation/Foundation.h>
@interface EventBO : NSObject
@property (nonatomic, strong) NSString *eventName;
@property (nonatomic, strong) NSString *eventImage;
@property (nonatomic, strong) NSDate *eventTime;
@end
在你的控制器中:
将全局可变数组称为NSMutableArray *arrEvents
分配数组(最好在viewDidLoad
)
arrEvents = [[NSMutableArray alloc] init];
然后在对象中分配Event(即eventName,eventTime和eventImage)的值,并在Global数组中添加对象
EventBO *objEvent = [[EventBO alloc]init];
objEvent.eventName = @"test";
objEvent.eventImage = @"test.png";
objEvent.eventTime = [NSDate date]; //You can put your time, I have set current date-time
[arrEvents addObject:objEvent]
现在使用表中的数组来获取对象并显示数据。