- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
经常当我拉起 UITableView
来刷新从服务器提取的内容时,它会崩溃并显示以下消息:
Terminating app due to uncaught exception 'NSRangeException', reason:
'-[__NSCFArray objectAtIndex:]: index (4) beyond bounds (4)'
现在的问题是,我这边的数据每次都被设置到 NSDictionary
中,并且每次都有相同的数据,所以我知道我的服务器不会返回任何不合适的内容。
这里一切都是如何工作的,我有一个名为 i
的整数。 i
在细胞重新填充时将始终 = 0,基于:
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
//end of loading
//for example [activityIndicator stopAnimating];
i = 0;//<!-- reset the i so that when page reloads cells it does not fail
}
}
我还为我的 NSDictionary
添加了 2
个额外插槽,因为插槽 0
和 1
是至关重要的部分。与填充前 2 个单元格的用户信息相关联:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self.dataGrabed_dictionary objectForKey:@"scheduled_times"] count] + 2;//<!-- by default we want to make sure 2 is being added no matter what. 1 = clients address and name, 2 = upcoming lawn care
}
所以回到i
对cellForRowAtIndexPath
的前2次调用,我使用if语句
为前2个单元格分配收集的数据。当我用数据填充其他单元格(如果有)
i
这正是 cellForRowAtIndexPath
中发生的情况:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%d",i);
self.tableView.separatorColor = [UIColor clearColor];// Get rid of speration border color
static NSString *CellIdentifier = @"homecell";
HomePageTimelineCell *cell = (HomePageTimelineCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[HomePageTimelineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell initTimelineCell];
cell.backgroundColor = [UIColor clearColor];// Set the background color to the cell
// lets check if there is any data
if([self.dataGrabed_dictionary objectForKey:@"scheduled_times"] == nil && [self.dataGrabed_dictionary objectForKey:@"upcoming"] == nil)
{
//<!-- if the indexRow row = 0, this is the clients username and address
if(indexPath.row == 0)
{
NSArray *get = [[SSKeychain allAccounts] init];
NSString *username = [get[0] objectForKey:@"acct"];
//<!-- Get keyname of the address and than point to that keyname and get data
//NSString *KeyName = [[self.dataGrabed_dictionary allKeys] objectAtIndex: indexPath.row];
//<!-- create uppercase strings
NSString *upperCaseAddress = [[self.dataGrabed_dictionary objectForKey:@"client_address"] uppercaseString];
NSString *upperCaseUsername = [username uppercaseString];
//<!-- Set associated strings
cell.addressLabel.text = upperCaseAddress;
cell.usernameLabel.text = upperCaseUsername;
}
else if(indexPath.row == 1)
{
cell.contentView.backgroundColor = [self colorWithHexString:@"666666"];
cell.button.backgroundColor = [self colorWithHexString:@"7ddcb8"];
cell.button.tag = indexPath.row;
[cell.button setTitle:@"schedule" forState:UIControlStateNormal];
[cell.button setTitleColor:[self colorWithHexString:@"666666"] forState:UIControlStateNormal];
/* Setup button action */
//[cell.button addTarget:self action:@selector(onCustomAccessoryTapped:) forControlEvents:UIControlEventTouchUpInside];
cell.dateLabel.tag = indexPath.row + 100; //<!-- 100 = date
cell.dateLabel.text = @"Nothing Scheduled";//<!-- Set the Date
[cell.contentView addSubview:cell.button];
[cell.contentView addSubview:cell.nextLabel];
[cell.contentView addSubview:cell.dayLabel];
[cell.contentView addSubview:cell.dateLabel];
}
}
else
{
//<!-- Set background color
if (indexPath.row % 2) {
cell.contentView.backgroundColor = [self colorWithHexString:@"77d1af"];
//<!-- Set background and text color for even cells
[cell.button setTitleColor:[self colorWithHexString:@"7ddcb8"] forState:UIControlStateNormal];
cell.button.backgroundColor = [self colorWithHexString:@"666666"];
} else {
cell.contentView.backgroundColor = [self colorWithHexString:@"7ddcb8"];
//<!-- Set background and text color for even cells
[cell.button setTitleColor:[self colorWithHexString:@"7ddcb8"] forState:UIControlStateNormal];
cell.button.backgroundColor = [self colorWithHexString:@"ffffff"];
}
//<!-- if the indexRow row = 0, this is the clients username and address
if(indexPath.row == 0)
{
NSArray *get = [[SSKeychain allAccounts] init];
NSString *username = [get[0] objectForKey:@"acct"];
//<!-- Get keyname of the address and than point to that keyname and get data
//NSString *KeyName = [[self.dataGrabed_dictionary allKeys] objectAtIndex: indexPath.row + 4];
//<!-- create uppercase strings
NSString *upperCaseAddress = [[self.dataGrabed_dictionary objectForKey:@"client_address"] uppercaseString];
NSString *upperCaseUsername = [username uppercaseString];
//<!-- Set associated strings
cell.addressLabel.text = upperCaseAddress;
cell.usernameLabel.text = upperCaseUsername;
}
//<!-- if the indexRow row = 1, this is the 2 cell and will show the most upcoming lawn schedule
else if(indexPath.row == 1)
{
//<!-- Lets check if user even has an upcoming mowing date -->
if([self.dataGrabed_dictionary objectForKey:@"upcoming"] == nil)
{
cell.contentView.backgroundColor = [self colorWithHexString:@"666666"];
cell.button.backgroundColor = [self colorWithHexString:@"7ddcb8"];
cell.button.tag = indexPath.row;
[cell.button setTitle:@"schedule" forState:UIControlStateNormal];
[cell.button setTitleColor:[self colorWithHexString:@"666666"] forState:UIControlStateNormal];
/* Setup button action */
//[cell.button addTarget:self action:@selector(onCustomAccessoryTapped:) forControlEvents:UIControlEventTouchUpInside];
cell.dateLabel.tag = indexPath.row + 100; //<!-- 100 = date
cell.dateLabel.text = @"Nothing Scheduled";//<!-- Set the Date
[cell.contentView addSubview:cell.button];
[cell.contentView addSubview:cell.nextLabel];
[cell.contentView addSubview:cell.dayLabel];
[cell.contentView addSubview:cell.dateLabel];
}
else
{
//<!-- create uppercase strings
NSString *upperCaseDay = [[self.dataGrabed_dictionary objectForKey:@"upcoming_day"] uppercaseString];
//<!-- create uppercase strings
NSString *upperCaseDate = [[self.dataGrabed_dictionary objectForKey:@"upcoming_date"] uppercaseString];
cell.contentView.backgroundColor = [self colorWithHexString:@"666666"];
cell.button.backgroundColor = [self colorWithHexString:@"7ddcb8"];
cell.button.tag = indexPath.row;
[cell.button setTitle:@"change" forState:UIControlStateNormal];
[cell.button setTitleColor:[self colorWithHexString:@"666666"] forState:UIControlStateNormal];
/* Setup button action */
//[cell.button addTarget:self action:@selector(onCustomAccessoryTapped:) forControlEvents:UIControlEventTouchUpInside];
cell.dayLabel.text = upperCaseDay;//<!-- Set the day
cell.dateLabel.tag = indexPath.row + 100; //<!-- 100 = date
cell.dateLabel.text = upperCaseDate;//<!-- Set the Date
[cell.contentView addSubview:cell.button];
[cell.contentView addSubview:cell.nextLabel];
[cell.contentView addSubview:cell.dayLabel];
[cell.contentView addSubview:cell.dateLabel];
}
}
else //<!-- Normal cell population
{
//<!-- Re edit labels positioning
cell.dayLabel.frame = CGRectMake(9, 10, 200, 45);
cell.dateLabel.frame = CGRectMake(9, 35, 300, 45);
//<!-- Setup data called from server
NSDictionary *innerClientData =[self.dataGrabed_dictionary objectForKey:@"scheduled_times"][i];
NSString *innerClientDay =[self.dataGrabed_dictionary objectForKey:@"scheduled_times_day"][i];
NSString *innerClientDate =[self.dataGrabed_dictionary objectForKey:@"scheduled_times_date"][i];
//<!-- create uppercase strings
NSString *upperCaseDay = [innerClientDay uppercaseString];
//<!-- create uppercase strings
NSString *upperCaseDate = [ innerClientDate uppercaseString];
//<!-- Check to see if client paid
if([[innerClientData objectForKey:@"client_paid"] isEqual: @"0"])
{
NSString *amount = [innerClientData objectForKey:@"client_price"];
NSString *pay = @"pay $";
NSString * combined = [pay stringByAppendingString:amount];
[cell.button setTitle:combined forState:UIControlStateNormal];
}
else if([[innerClientData objectForKey:@"client_paid"] isEqual: @"1"])
{
[cell.button setTitle:@"PAID" forState:UIControlStateNormal];
}
//[cell.button setTitleColor:[self colorWithHexString:@"666666"] forState:UIControlStateNormal];
cell.button.tag = indexPath.row;
/* Setup button action */
//[cell.button addTarget:self action:@selector(onCustomAccessoryTapped:) forControlEvents:UIControlEventTouchUpInside];
cell.dayLabel.text = upperCaseDay;//<!-- Set the day
cell.dateLabel.tag = indexPath.row + 100; //<!-- 100 = date
cell.dateLabel.text = upperCaseDate;//<!-- Set the Date
[cell.contentView addSubview:cell.button];
[cell.contentView addSubview:cell.dayLabel];
[cell.contentView addSubview:cell.dateLabel];
i++;
}
}
return cell;
}
我对 NSDictionary
的分配如下所示:
NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"responseString: %@",responseString);
// GRAB STATUS OBJECT
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:returnData //1
options:kNilOptions
error:&error];
self.dataGrabed_dictionary = [json objectForKey:@"retrieved_data"];
关于为什么它会因越界
而崩溃的建议?
最佳答案
最有可能的问题是您获取单元格数据的方式。您在以下行中使用了实例变量 i
:
NSDictionary *innerClientData =[self.dataGrabed_dictionary objectForKey:@"scheduled_times"][i];
NSString *innerClientDay =[self.dataGrabed_dictionary objectForKey:@"scheduled_times_day"][i];
NSString *innerClientDate =[self.dataGrabed_dictionary objectForKey:@"scheduled_times_date"][i];
那永远行不通。将 i
替换为 indexPath.row - 2
。
单元格可以按不同的顺序多次访问。
关于ios - cellForRowAtIndexPath 中的索引频繁出界 - Obj C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27975447/
我有一个包含值的字符串数组(duh...)。 有没有一种简单的方法可以获取出现次数最多的条目?有点像 values[37].getMostOften(); 干杯:) 最佳答案 您可以使用GroupBy
我目前正在将一款用 C#(适用于 Windows Phone)开发的游戏移植到 Java (Android)。 我们在 Java 版本中遇到了内存问题,在分析之后,似乎是由于内存中的大量 String
对于播放音频文件的 iPhone 应用程序,我正在开发一个系统来跟踪用户在他们听过的任何一集中的进度(例如,他们听 file1 的前 4:35,然后开始另一个文件,然后返回到文件 1,它从 4:35
如果您按下 UIbutton 显示 UITextView,将请求代码 Ì 再次按下 UIbutton 再次显示 UITextView :/ 最佳答案 .h 文件中只有一个 int 变量,如下所示..
我在 Application_End 上处理的项目中使用临时数据库: protected void Application_End() { if (_db != null) _db.Dispo
我是一名优秀的程序员,十分优秀!